Code, again:
console.log( field.$el.on('keyup change', function() {
console.log( field.$el.val().length );
if( field.$el.val().length > 0 ) field.clearError();
}), $._data( field.$el[0], 'events' ) );
The element exists at time of binding. The events appear to bind according to $._data
, but when typing in the input, nothing happens. No console output as expected, the function is not called, and no console errors. If I use element.addEventListener
instead it works fine.
Any ideas why this is broken? Using jQuery 3.2.1.
Edit: After the page loads, running $._data( $('#input_creative_url')[0], 'events' )
from console on the element in question returns undefined
rather than the same event output from the console at runtime. What would cause jQuery to clear its internal data for this element?
Edit 2: Rather than using $(document).ready(...)
, I have the code placed inline after the elements referenced by the code. I'm doing this for performance reasons - $(document).ready(...)
takes too long to trigger. If I wrap the code in a $(document).ready(...)
the code works fine. It appears that, in the original setup, jQuery is deleting the bound events on $(document).ready(...)
. Is there any way to prevent this?
Edit 3: If, in the original example, I prepend the code with jQuery.cleanData = function(){};
, the code works fine. So jQuery.cleanData
and/or its callee's are the culprit. Obviously, I can't just blow up jQuery's garbage collection to make my code work, but does anyone have any recommendations on where to go from here?
Edit 4: I figured it out. I was removing the element's parent from the DOM and re-inserting it elsewhere. I need to use detach()
instead of remove()
.