Board index » javascript » addEventListener/attachEvent, why doesn't this work?
|
Bert
Registered User |
addEventListener/attachEvent, why doesn't this work?
2005-12-20 07:49:05 PM
Hello, I'm having some problems with creating a script that works on both Mozilla browsers as IE. I want to change the background color of textareas and input text fields as soon as somebody has entered something in them. The script below works perfectly in mozilla browsers (netscape 7, firefox 1,...) but doesn't in internet explorer (but it gives no errors) Any ideas why the attachEvent doesn't work? I've pasted the script below. thank you, Bert <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head><title>Problem</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> var pressedkey=''; document.onkeypress = function (evt) { pressedkey = document.layers ? evt.which : document.all ? event.keyCode : evt.keyCode; return true; }; function notify_change() { //prevent TAB key from changing background if(pressedkey != 9) this.style.background='#FFC'; } </script> </head> <body> <form name="simplified_form" method="post" action="#"> <textarea name="ggg_id5" id="ggg_id5" cols="100" rows="3">Change this</textarea><br/> <input type="text" name="hhh_id9" id="hhh_id9" size="16" value="Value 1" /><br/> <input type="text" name="hhh_id10" id="hhh_id10" size="16" value="Value 2" /> <div align="center"><input type="reset"><input type="submit"></div> </form> <script language="JavaScript" type="text/JavaScript"> var inputs = document.getElementsByTagName("INPUT"); var textareas = document.getElementsByTagName("TEXTAREA"); for (i = 0; i < textareas.length; i++) { if(textareas[i].addEventListener) { textareas[i].addEventListener("keyup", notify_change, false); } else { //Internet explorer textareas[i].attachEvent("keyup", notify_change); } } for (i = 0; i < inputs.length; i++) { if(inputs[i].addEventListener ) { inputs[i].addEventListener("keyup", notify_change, false); } else { //Internet explorer inputs[i].attachEvent("keyup", notify_change); } } </script> </body> </html> - |
