Created
December 5, 2013 18:34
-
-
Save mpouncy-netpulse/7810757 to your computer and use it in GitHub Desktop.
jQuery Text Change Event Plugin. Simple cross browser detection of text changes for input and textarea elements using a jQuery custom event plugin. SEE: http://www.zurb.com/playground/jquery-text-change-custom-event. Adapted to make debugging a bit easier.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* jQuery TextChange Plugin | |
* http://www.zurb.com/playground/jquery-text-change-custom-event | |
* | |
* Copyright 2010, ZURB | |
* Released under the MIT License | |
*/ | |
;(function(a) { | |
a.event.special.textchange = { | |
setup : function textchangeSetup() { | |
a(this).data("lastValue", "true" === this.contentEditable ? a(this).html() : a(this).val()); | |
a(this).bind("keyup.textchange", a.event.special.textchange.handler); | |
a(this).bind("cut.textchange paste.textchange input.textchange", a.event.special.textchange.delayedHandler); | |
}, | |
teardown : function textchangeTeardown() { a(this).unbind(".textchange"); }, | |
handler : function textchangeHandler() { a.event.special.textchange.triggerIfChanged(a(this)); }, | |
delayedHandler : function textchangeDelayedHandler() { | |
var c = a(this); | |
setTimeout(function textchangeDelayedHandlerTimeout() { a.event.special.textchange.triggerIfChanged(c); }, 25); | |
}, | |
triggerIfChanged : function textchangeTriggerIfChanged(a) { | |
var b = "true" === a[0].contentEditable ? a.html() : a.val(); | |
b !== a.data("lastValue") && (a.trigger("textchange", [a.data("lastValue")]), a.data("lastValue", b)); | |
} | |
}; | |
a.event.special.hastext = { | |
setup : function hastextSetup() { a(this).bind("textchange", a.event.special.hastext.handler); }, | |
teardown : function hastextTeardown() { a(this).unbind("textchange", a.event.special.hastext.handler); }, | |
handler : function hastextHandler(c, b) { "" === b && b !== a(this).val() && a(this).trigger("hastext"); } | |
}; | |
a.event.special.notext = { | |
setup : function notextSetup() { a(this).bind("textchange", a.event.special.notext.handler); }, | |
teardown : function notextTeardown() { a(this).unbind("textchange", a.event.special.notext.handler); }, | |
handler : function notextHandler(c, b) { "" === a(this).val() && a(this).val() !== b && a(this).trigger("notext"); } | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment