I've noticed that the order of 'click' and 'change' events are different in Chrome and Firefox.
See this JSFiddle for example: /
JavaScript:
var el = $('foo');
var fn = function(e) {
console.log(e.type);
}
el.addEvent('change', fn);
el.addEvent('click', fn);
In Chrome this logs:
change
click
And in Firefox this logs:
click
change
Is there a standard for the order of events? Which should fire first? The MDN doesn't seem to mention this and I couldn't find a thing about this in the W3C documents.
I've noticed that the order of 'click' and 'change' events are different in Chrome and Firefox.
See this JSFiddle for example: http://jsfiddle.net/5jz2g/3/
JavaScript:
var el = $('foo');
var fn = function(e) {
console.log(e.type);
}
el.addEvent('change', fn);
el.addEvent('click', fn);
In Chrome this logs:
change
click
And in Firefox this logs:
click
change
Is there a standard for the order of events? Which should fire first? The MDN doesn't seem to mention this and I couldn't find a thing about this in the W3C documents.
Share Improve this question edited Mar 22, 2020 at 23:00 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Jul 29, 2014 at 12:06 stroborobostroborobo 4581 gold badge4 silver badges10 bronze badges 10- 10 If it's not mentioned, I would not rely on it. Same thing for the blur event. As a side note, you should surely never rely on one listener being called before the other (when you register two listeners for the same event, since that is specified as undefined) stackoverflow.com/questions/2706109/… – Ruan Mendes Commented Jul 29, 2014 at 12:10
- 1 It is not relevant for checkboxes at least, you need to listen only change event – micnic Commented Jul 29, 2014 at 12:11
- 3 If your architecture design relies on those events order, then 99% that you should reconsider that architecture – Alma Do Commented Jul 29, 2014 at 12:12
- 3 @micnic the reason "change" event is problematic is behavior of older versions of Internet Explorer. They do not fire the "change" event until the element loses focus. – Pointy Commented Jul 29, 2014 at 12:13
- 2 IMHO, FF is doing it right. It's similar to the relationship between the click event on a form's submit button and the form's submit event. – Barmar Commented Jul 29, 2014 at 12:22
4 Answers
Reset to default 9DOM3 Events document has a recommendation about events order. According to it, in case of checkbox, the correct order will be click
then change
and not vice versa, because change
event obviously is a part of default actions for checkbox, see Example 5 and fiddle, which works as expected in FF but not in Chrome. That's logical, anyway. But! Let's read default actions section carefully:
Default actions should be performed after the event dispatch has been completed, but in exceptional cases may also be performed immediately before the event is dispatched.
Did you see that? W3C uses RFC's words SHOULD and MAY in one sentence! Nothing to be done, they're cautious guys. IMO, that's why we have what we have :)
No, but if you want to make it consistent, you can fire one event off the other rather than having them fire separately since for a check box and change amount to the same event...or perhaps you could use an interval timer to to delay the response on one of the events.
try this
$(function(){
var $select = $('foo');
var store = function(e) {
console.log(e.type);
};
$select.addEvent('change', store).addEvent('click', store);
});
http://jsfiddle.net/donddoug/Du7z5/
Couldn't find a way to sync events cross browser...
the following does work in the same order in both FF and Chrome;
JavaScript Code (use mouseup / mousedown to catch the "click"):
var el = $('foo');
var fn = function(e) {
console.log(e.type);
}
el.addEvent('change', fn);
el.addEvent('mouseup', fn);
http://jsfiddle.net/5jz2g/17/