J
J
jtag2018-01-28 18:26:47
JavaScript
jtag, 2018-01-28 18:26:47

How to stop the event handler?

If you click icon1 then icon2 and map, then both map handlers work. How to stop icon1 event handler when icon2 is clicked?

$('.icon1').click(function(event){ 
     $('.map').click(function(event){ 
          console.log("from icon1");
     });
});

$('.icon2').click(function(event){ 
     $('.map').click(function(event){ 
          console.log("from icon2");
     });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-01-28
@msa6886

var $map = $('.map');

$('.icon1').click(function(event){
  $map.off('click'); 
  $map.click(function(event){ 
    console.log("from icon1");
  });
});

$('.icon2').click(function(event){ 
  $map.off('click'); 
  $map.click(function(event){ 
    console.log("from icon2");
  });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question