Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix for IE not firing the change event for certain inputs (namely, checkboxes and multiple select lists).
/**
* Fixes binding the "change" event to checkboxes and select[type=multiple]
* for Internet Explorer.
*
* @param {jQuery|Element|Element[]} elements
* The DOM Element we wish to bind the event to.
*
* @param {String} eventType
* The name of the event we want to bind to.
*
* @param {function} callback
* The function to execute when the event is triggered
*/
var bind = function(elements, eventType, callback) {
var $elements = $(elements),
rValidProps = /^(checked|selectedIndex)$/,
hasPropertyChange = ("onpropertychange" in document.body);
if (!$elements.length || typeof eventType !== "string") {
return $elements;
}
if (eventType !== "change") {
return $elements.bind(eventType, callback);
}
$elements.each(function() {
eventType = hasPropertyChange && (this.type === "checkbox" || this.tagName.toLowerCase() === "select" && this.multiple) ? "propertychange" : "change";
$(this).bind(eventType, function(e) {
if (e.type !== "propertychange" || rValidProps.test(window.event.propertyName)) {
callback.call(this, e);
}
});
});
};
@kflorence

This comment has been minimized.

Copy link
Owner Author

commented Jan 8, 2011

@kflorence

This comment has been minimized.

Copy link
Owner Author

commented Jan 8, 2011

Note: this seems to have been fixed in jQuery version 1.4.2, although select[multiple] elements still seem to be acting funny (it only fires every other change for some reason).

@kflorence

This comment has been minimized.

Copy link
Owner Author

commented Jan 8, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.