Skip to content

Instantly share code, notes, and snippets.

@dantman
Created December 16, 2010 17:51
Show Gist options
  • Save dantman/743725 to your computer and use it in GitHub Desktop.
Save dantman/743725 to your computer and use it in GitHub Desktop.
/** Implementation of HTML5 input event **/
/*jQuery.support.inputEvent = false;
(function() {
var el = document.createElement("div");
if ("oninput" in el)
jQuery.support.inputEvent = true;
if ( !jQuery.support.inputEvent ) {
el.setAttribute("oninput", "return;");
if ( typeof el.oninput === "function" )
jQuery.support.inputEvent = true;
}
el = null;
})();*/
jQuery.fn.input = function( fn ) {
return fn ? this.bind( "input", fn ) : this.trigger( "input" );
};
if ( jQuery.attrFn )
jQuery.attrFn.input = true;
/*if ( !jQuery.support.inputEvent ) {
function inputFocus(e) {
// Set oldinputvalue as a base when the user focuses the input
jQuery.data(this, "_input_data", jQuery(this).val());
}
function inputApply(e) {
jQuery.removeData(this, "_input_timeout");
var value = $(this).val();
var oldValue = jQuery.data(this, "_input_data");
if ( value === oldValue )
return; // Always skip calling the event when data is the same
jQuery.data(this, "_input_data", value);
e.type = "input";
jQuery.event.handle.apply(this, arguments);
}
function inputEvent(e) {
var timeout = jQuery.data(this, "_input_timeout");
// Input events are required to have a delay to them, the html5 spec
// lists an 100ms delay as an example
// To accomidate this we set a timeout and add it as a data value
// If a change event fires we automatically clear this timeout and
// apply the event
// This is a minor violation of the html5 spec because input events
// are supposed to fire BEFORE change events, but that shouldn't matter
// that much to us
if ( e.type === "change" ) {
if ( timeout )
clearTimeout(timeout);
jQuery.removeData(this, "_input_timeout");
inputApply.call(this, e);
return;
}
//if ( !timeout )
// jQuery.data(this, "_input_timeout", setTimeout(function() { inputApply.call(this, e); }, 5000));
}
jQuery.event.special.input = {
setup: function() {
jQuery.event.add(this, "focusin", inputFocus);
jQuery.event.add(this, "keyup", inputEvent);
jQuery.event.add(this, "blur", inputEvent);
jQuery.event.add(this, "change", inputEvent);
},
teardown: function() {
jQuery.event.remove(this, "focusin", inputFocus);
jQuery.event.remove(this, "keyup", inputEvent);
jQuery.event.remove(this, "blur", inputEvent);
jQuery.event.remove(this, "change", inputEvent);
}
};
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment