Tutorial :Using .onKeyUp and .onChange with Javascript DOM objects



Question:

I'm having a weird problem with some Javascript/DOM code I've ben playing with. I'm trying to assign the .onKeyUp and .onChange events/methods to a text input like so:

form.elements["article"].onkeyup = "alert('test');";  

Oddly, assigning using that method is doing nothing, and I'm forced to do this manually using:

form.elements["article"].setAttribute("onkeyup", "alert('test');");  

Am I missing something here? I've used the first method I mentioned before and it has worked fine. Thanks for your help!


Solution:1

Try this:

form.elements["article"].onkeyup = function() { alert("test"); };  

Steve


Solution:2

You need to assign a function, not a string. For example:

form.elements["article"].onkeyup = function(){alert('test');};  

The only things I know that will take a string and eval it (other than eval) are setTimeout and setInterval.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »