I came across code that adds custom events to detect input text changes originating from either a keyboard or mouse event:
http://www.zurb.com/playground/jquery-text-change-custom-event
The code is a bit older, and the documentation says to use bind()
to attach the custom events, e.g.:
$('#exhibita').bind('hastext', function () {
$('#exhibitaButton').removeClass('disabled').attr('disabled', false);
});
The jQuery docs state
As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements.
I thought I could just rewrite the code as:
$(document).on('hastext', '#exhibita', function () {
$('#exhibitaButton').removeClass('disabled').attr('disabled', false);
});
but the event handler is never triggered.
Questions
- Did I make a mistake in my use of
on()
? - Must the code providing the custom events be rewritten to support
on()
? If so, what must change? - Is
bind()
documented to be a candidate for removal in a future version of jQuery? I did not see a depreciation warning in the docs, but I knowlive()
anddie()
were removed from 1.9... - Is there a future-proof way to use these custom events without rewriting the code?