How can I disable jQuery event handler?

  • 0
    Hello, can you please tell me how to disable the event handler for a specific element?
    Here is the code:
    <div class="pay-ways__tabs">
            <p data-tab="1" class="disabled">При получении</p>
    	<p data-tab="2" class="tab-active">Онлайн</p>
    <!-- Там дальше идут две формы с такими же data-tab атрибутами-->

    And here's the JS:
    $('.delivery__tabs p').bind('click', function() {
    		var dataTab = $(this).data('tab'),
    			tabContent = $('.pay__body form[data-tab="'+dataTab+'"]');
    		$('.pay__body form').removeClass('active');
    		$('.delivery__tabs p').removeClass('tab-active');
    	$('.delivery__tabs p.disabled').unbind('click');

    Here I wanted to remove the 'click' event handler using the .unbind () method, for the tab with the 'disabled' class, but why not remove it.
    JavaScript Anonymous, Dec 21, 2019

  • 1 Answers
  • 0
    Try this:

    $('.delivery__tabs p').bind('click', function() {
    if (typeof delivery_tabs_p == 'undefined' ) {
    console.log('Обработчик работает');

    As long as the delivery_tabs_p variable does not exist, the handler is running.

    To disable the handler, just write something to the variable:

    delivery_tabs_p = '1';

    I understand, the solution is so-so, but you don't have to delve into why the handler is not canceled there.

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!