[Javascript] Programmatically set onchange event for textbox

Nick Fitzsimons nick at nickfitz.co.uk
Wed Oct 4 08:10:51 CDT 2006


On 4 Oct 2006, at 02:56, Glenn E. Lanier, II wrote:

> var e = document.getElementById('txtCardNumber');
> if (e)
> {
>    alert("Textbox found");
>    e.onchange = "SelectCardType()";
> }
> 		
> function SelectCardType()
> {
>    alert("Card number typed");
> }
>

You are assigning the string "SelectCardType()" to the onchange  
property; as a string isn't a function, nothing will happen. You need  
to assign the function to the onchange property:

    e.onchange = SelectCardType;

The event handler will then be executed when the text field loses  
focus and its value has changed.

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/






More information about the Javascript mailing list