[Javascript] Programmatically set onchange event for textbox

Glenn E. Lanier, II glenn_lanier at netzero.net
Wed Oct 4 09:53:22 CDT 2006


> From: Nick Fitzsimons
> Sent: Wednesday, October 04, 2006 8:11 AM

> 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, you're spot on. I tried without the quotes (e.onchange =
SelectCardType();), but the function ran right then. Just tried it without
quotes or parens, and all is working. 

Now, just as a thought for future use, is is possible to pass parameters to
this function? Is so, how would that be done?

Thanks again.

--G




More information about the Javascript mailing list