views:

766

answers:

5

Hello

It might be a beginner question but I can't understand why the onchange event is never called by IE while it works Ok with firefox.

<input type="text" id="mytext" size="48" value="" onchange="execute()"/>
<button type="button" onclick="execute()">Go</button>

The execute function is called when the button is clicked but not when the text in the input box is changed.

Any idea?

+1  A: 

IE does it after your input loses focus, which isn't until you click the button, tab out, or click somewhere else on the screen. Try onclick or one of the other events.

Scott Saunders
+8  A: 

IE only fires the onchange event when the element loses focus - if you were to click outside the element or tab to a different element it should fire then.

You can get around this by using a different even, for example onkeypress.

Greg
Sure that only IE shows this behaviour? I just tested it in FF3.5 and IE7 - both show the same behaviour i.e. both fire the event after losing focus.
msparer
After some tries, I've to admit that this is the best answer. jquery have a similar issue with IE. I had to use keydown event rather than onchange.
luc
As usual, reading the specs is a good place to start: *"The change event occurs when a control loses the input focus and its value has been modified since gaining focus."* - http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents
NickFitz
It's usually a good idea to use both `onkeypress` and `onchange` since the field can be modified with right click+paste as well.
Justin Johnson
+2  A: 

As far as i remember, IE doesn't handle onchange event the same maner as FF. The event will be fired when the mouse is clicked.

I advise you to use a library to handle events such as jQuery, Dojo, etc..

Boris Guéry
You are right! and I will follow your advice and use jquery for that
luc
jQuery is a good recommendation but it has a similar issue with onchange and IE
luc
Could you post your code ? I did recently a similar thing to handle two select inputs and had no problems.
Boris Guéry
+1  A: 

ohhh, I spent some time on that issue as well months ago. I came up with this solution for FF/IE onchange

$("input[name*='delivery_method']").bind(($.browser.msie ? "click" : "change"), function() {
    //your code here
});
Avi Tzurel
+1  A: 

It is actually not a bug that onchange is not fired until the element loses focus. However, it is annoying. I get around the issue by having multiple bindings for different events; make sure not to clobber a handler and use an update aggregation if appropriate.

Here the "official" W3C documentation on the subject: http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.3:
"""The onchange event occurs when a control loses the input focus and its value has been modified since gaining focus. This attribute applies to the following elements: INPUT, SELECT, and TEXTAREA."""

Here is the MSDN reference: http://msdn.microsoft.com/en-us/library/ms536912%28VS.85%29.aspx:
"""This event is fired when the contents are committed and not while the value is changing. For example, on a text box, this event is not fired while the user is typing, but rather when the user commits the change by leaving the text box that has focus."""

The behavior, while often annoying, is as specified.

pst