Jquery onclick click event

I have a div that has an onlcick event. Now when I click div, I want to change my onlick function using the .click jquery attribute. I can change it dynamically, but when I apply it, a new event is also fired. Is there any work to prevent the new one from being launched, but just applied to the div with the new function? This is what i am trying to do

i has a div

<script type="text/javascript">

function testMe(data)
{
   alert(data);
   $('#testMe').unbind('click');
   $('#testMe').click(function(){ testMe('2');  });
}

</script>
    <div id='testMe' onclick='testMe(1)' >click on me, I will disappear.</div>

when I execute the code above, I still get the value 1 ohm warning field THANKS EveryOne

I also tried the code below, but it does not work for me

 function testMe(data)
{
   alert(data);
   $('#testMe').unbind('click');
   $('#testMe').click( testMe(2) );
}

Although the code updates the onlick event from 'testMe (1)' to 'testMe (2)', it continuously alerts the value 2. Working around this problem is a traditional JS script code like:

 document.getElementById('testMe').onclick =  testMe(2) ;

And his work is without warning.

+3
4

one

$('element').one('click',function()
{
    alert('old click handler');
    $(this).click(function(){
        alert('new click handler, yarrrr');
    });
});
+5

, .

$('div').click(function(){
    $(this).unbind('click');
    $(this).click(function(){
        // new click function here
    });
});
+2

Try using the binding event http://api.jquery.com/bind/

If you want to remove the previous event, use the unbind event: Best way to remove an event handler in jQuery?

0
source

untie the old functions first. Than to tie a new event to him, he must do the trick.

Do you mean that the event also fires when you bind it to an element?

0
source

Source: https://habr.com/ru/post/1772133/


All Articles