Here is the code to add event
$(document).on({
click: function() {
$(this).hide();
$('#form_name').removeClass('hide');
$('#form_template_name')
.attr('placeholder', $(this).text())
.focus();
}
}, '.form-template-name');
For some conditions i dont want this event to trigger. So what I tried is
$('.form-template-name').off();
$('.form-template-name').unbind();
But nothing seems to work. Did I miss anything ?
jQuery unbind() Method Use the off() method instead. The unbind() method removes event handlers from selected elements. This method can remove all or selected event handlers, or stop specified functions from running when the event occurs. This method can also unbind event handlers using an event object.
Dynamically disable all clicks on page let freezeClic = false; // just modify that variable to disable all clics events document. addEventListener("click", e => { if (freezeClic) { e. stopPropagation(); e. preventDefault(); } }, true);
The off() method is most often used to remove event handlers attached with the on() method. As of jQuery version 1.7, the off() method is the new replacement for the unbind(), die() and undelegate() methods.
You need to pass the event to unbind to .off(), also see the use of namepsaced event names
$(document).on({
'click.myevent': function () {
$(this).hide();
$('#form_name').removeClass('hide');
$('#form_template_name')
.attr('placeholder', $(this).text())
.focus();
}
}, '.form-template-name');
and
$(document).off('click.myevent', '.form-template-name');
Demo: Fiddle
Try this.
$(document).off('click', '.form-template-name');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With