Jquery adding and removing event handlers

I have 2 buttons

 $(".button1").on("click", function(event){
 $(".button2").on("click", function(event){

What I would like to do is start from button1, by clicking and not listening to the event listener, when button2 is pressed, the event listener is deleted, and the event listener for button 1 is activated.

This is a fairly common method in actionscript 3 with the removeEventListener () method and would like to use a similar method or method in jquery.

+5
source share
3 answers

If you want to remove the event handler associated with jQuery.on, then you probably want tojQuery.off

http://api.jquery.com/off/

+7
source

on() bind event off() . off(), , .

    $('.button1').on('click', function() {
        console.log('button1');
        $(this).off('click');
        $('.button2').on('click');
    })
    $('.button2').on('click', function() {
         console.log('button2');
        $(this).off('click');
        $('.button1').on('click');
    })
+5

, ,

 $(".slide1").on("click", function(){
    $(this).addClass('on');
    $('.slide2').removeClass('on');
     $(this).off('click');
    $('.slide2').on('click');
});

$(".slide2").on("click", function(){
    $(this).addClass('on');
    $('.slide1').removeClass('on');
    $(this).off('click');
    $('.slide1').on('click');
});
0
source

All Articles