472,980 Members | 1,906 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 software developers and data experts.

addEventListener/attachEvent, why doesn't this work?

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>
Dec 20 '05 #1
5 14420
VK
<FAQENTRY>
addEventListener method requires event names in dis-on'ed form:

addEventListener("keypress", myFunction, false);

attachEvent method requires event names in on'ed form:

attachEvent("onkeypress", myFunction);
</FAQENTRY>

Also not that you're asking for troubles by scripting DOM object from
"immediate execution" script in the document body. More complex
document - higher the chances that the relevant DON objects will not be
ready for scripting (interactive) despite they are above the script in
the document flow.

You should always use window.onload to prepare the document.

Dec 20 '05 #2


Bert wrote:
function notify_change() {
//prevent TAB key from changing background
if(pressedkey != 9)
this.style.background='#FFC';
} inputs[i].attachEvent("keyup", notify_change);


In IE's event model if you use attachEvent and then when the function
(e.g. notify_change) passed to attachEvent is called the this object is
not set to the object the attachEvent has been called on.
Thus you attempts above to script
this.style.background
do not find any element node as you probably want. The this object whe
notify_change is called is simply the global object, the window object.
You would need to access e.g.
window.event.srcElement
in IE's object model to access an element node.

Furthermore attachEvent as the first argument wants what MS/IE
understands to be the event name and that is 'oneventname' e.g.
'onkeyup' and not 'keyup'.

I have not checked whether your other code setting that pressedkey
variable for instance make any sense when it comes to cross browser key
event handling.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 20 '05 #3
On Tue, 20 Dec 2005 13:27:36 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:
Bert wrote:
function notify_change() {
//prevent TAB key from changing background
if(pressedkey != 9)
this.style.background='#FFC';
}
inputs[i].attachEvent("keyup", notify_change);


In IE's event model if you use attachEvent and then when the function
(e.g. notify_change) passed to attachEvent is called the this object is
not set to the object the attachEvent has been called on.

[...]You would need to access e.g.
window.event.srcElement
in IE's object model to access an element node.
I will give this a try.

Furthermore attachEvent as the first argument wants what MS/IE
understands to be the event name and that is 'oneventname' e.g.
'onkeyup' and not 'keyup'.
OK :)
I have not checked whether your other code setting that pressedkey
variable for instance make any sense when it comes to cross browser key
event handling.


that was a quick hack, this code still needs some cleaning.

Thank you for your tips!
B.

Dec 20 '05 #4
On 20 Dec 2005 04:12:22 -0800, "VK" <sc**********@yahoo.com> wrote:
<FAQENTRY>
addEventListener method requires event names in dis-on'ed form:

addEventListener("keypress", myFunction, false);

attachEvent method requires event names in on'ed form:

attachEvent("onkeypress", myFunction);
</FAQENTRY>
Also not that you're asking for troubles by scripting DOM object from
"immediate execution" script in the document body. More complex
document - higher the chances that the relevant DON objects will not be
ready for scripting (interactive) despite they are above the script in
the document flow.

You should always use window.onload to prepare the document.


I've changed the script to use an onload.
Thanks,
B.
Dec 20 '05 #5
VK wrote:
You should always use window.onload to prepare the document.


No, you should use the `onload' attribute or `load' event, for
`window.onload' is proprietary.
PointedEars
Dec 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Joe Kelsey | last post by:
When you use addEventListener (or addEvent in IE) to call an object method, does it call it with the correct this parameter? The ECMAScript reference has a lot to say about the caller using...
4
by: PJ | last post by:
Is it possible to extend the Node object so that the DOM function addEventListener can be used w/ IE? Does anyone have an example of this? Thanks, Paul
1
by: philjhanna | last post by:
Hi I'm having a problem applying return false to a div via addEventListener. I'm adding this so that I can drag (click-hold-move) over an image. (Its so that I can add zooming to the image)...
4
by: Liming | last post by:
Hello all, I have a custom class object with some prototype methods like setKeyDownEvent, etc. The problem is on my webpage, after I instantiate the class, I try to do .addEventLister() to a...
2
by: rhamlin | last post by:
I'm working on a class. When instantiating I pass the variable name referencing the class (vw) and the page object where the class will display (view). In this class I create a DIV. I also...
3
by: Jake Barnes | last post by:
37 Signals has built some awesome software with some features I wish I knew how to imitate. When I'm logged into my page (http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver I'm...
10
by: Peter Michaux | last post by:
Hi, Today I have been testing the event models from Netscape 4.8 and IE 4 to the current crop of browsers. I'd like to write a small event library similar in purpose to the Yahoo! UI event...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
0
by: wpjoju | last post by:
i have this code which adds an event listener to a newly opened window but it doesn't seem to work in google chrome. the problem is window.addEventListener seem to work in chrome but if you do...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.