jQuery event handler .on() not working

The problem is that jQuery(".date_picker_disabled") finds elements with that class and binds to them. If elements don’t have the class at the time the binding is made, the events will not be handled.

The on function allows you to get round this by handling them on another element when the event “bubbles up to” a parent element. In this instance, we could say the body element – there may be a more specific common parent you could choose.

jQuery(document.body).on('click', '.date_picker_disabled', function(event) {
    alert('hi');
});

The event handler is now bound to the document.body element. All clicks that happen anywhere in the body are tested to see if they originated from an element matching the selector. If so, the handler is fired.

This is explained on the documentation for the on function. It is the same behaviour as was present in previous versions of jQuery with live and delegate functions.


Having taken another look at your code, you have disabled="disabled" set on your input element. click events are not fired on disabled elements.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)