Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove $(document).on click event?

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 ?

like image 564
Siva Avatar asked Mar 14 '14 09:03

Siva


People also ask

How do you unbind a click event?

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.

How do I turn off all click events?

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);

What is off click in jQuery?

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.


2 Answers

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

like image 96
Arun P Johny Avatar answered Sep 22 '22 03:09

Arun P Johny


Try this.

$(document).off('click', '.form-template-name');
like image 39
Rakesh Kumar Avatar answered Sep 20 '22 03:09

Rakesh Kumar