Skip to content

Instantly share code, notes, and snippets.

@cpojer
Forked from ibolmo/Element.Event.js
Created August 15, 2011 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpojer/1148139 to your computer and use it in GitHub Desktop.
Save cpojer/1148139 to your computer and use it in GitHub Desktop.
/*
---
name: Element.Event.Change
description: Normalizes the onchange-event in Internet Explorer for checkboxes and radio buttons.
license: MIT-style license.
requires: [Element.Event]
provides: Element.Event.Change
...
*/
/*<ltIE9>*/
if (!window.addEventListener) (function(){
var getValue = function(element){
var type = element.type;
return element[type == 'radio' || type == 'checkbox' ? 'checked' : 'value'];
};
var hasChanged = function(event){
var target = event.target,
value = target.retrieve('$change').value;
if (value == null || value == getValue(target)) return false;
switch (event.key){
case 'up': case 'down': case 'left': case 'right': return target.type == 'radio';
case 'space': return target.type == 'checkbox';
}
}
Element.Events.change = {
base: function(){
var type = this.type;
return (type == 'checkbox' || type == 'radio') ? 'click' : 'change';
},
condition: function(event){
return (event.target.type == 'radio' ? (event.type == 'keyup' ? !target.checked : target.checked) : true);
},
onAdd: function(fn){
var update = function(event){
event.target.store('$change', {
type: event.type,
value: getValue(event.target)
});
};
this.addEvents(this.retrieve('$changeEvents', {
keyup: function(event){
if (hasChanged(event)) fn.call(this, event);
},
focus: update,
keydown: update
});
},
onRemove: function(){
this.removeEvents(this.retrieve('$changeEvents')).eliminate('$changeEvents');
}
};
})();
/*</ltIE9>*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment