Skip to content

Instantly share code, notes, and snippets.

@csuwildcat
Forked from ibolmo/Element.Event.js
Created August 19, 2011 17:44
Show Gist options
  • Select an option

  • Save csuwildcat/1157453 to your computer and use it in GitHub Desktop.

Select an option

Save csuwildcat/1157453 to your computer and use it in GitHub Desktop.
/*
---
name: Element.Events.change
description: Contains Element methods for normalizing change events across browsers for checkboxes and radios.
license: MIT-style license.
requires: Element.Event
provides: Element.Events.change
...
*/
(function(){
var hasListener = !!window.addEventListener;
var update = function(event){
event.target.store('$change', {
type: event.type,
value: event.target.checked
});
};
Element.Events.change = {
base: 'click',
condition: function(event){
var target = event.target;
if (target.get('type') != 'radio') return true;
return (event.type == 'keyup') ? !target.checked : target.checked;
},
onAdd: function(fn){
var type = this.get('type');
if (type == 'checkbox' || type == 'radio') this.addEvents(this.retrieve('$change:events', {
focus: update,
keydown: update,
keyup: function(event){
var code = event.which || event.keyCode, element = event.target, value = element.retrieve('$change').value;
if (!hasListener && value != null && value != element.checked){
if ((type == 'checkbox' && code == 32) || (type == 'radio' && code >= 37 && code <= 39)) fn.call(this, event)
}
}
});
},
onRemove: function(){
this.removeEvents(this.retrieve('$change:events')).eliminate('$change:events');
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment