How to disable the onclick event once it happens?

21,234

Solution 1

If you are dealing this on jquery side,you can use jquery one method

$(".myDiv").one("click", function(){  
    alert("this will happen only once");  
});

Cheers

Solution 2

<img src="images/add.png" onclick="add_cart(); this.onclick=null;">

Solution 3

Using

<img src="images/add.png" onclick="this.onclick = function(){};add_cart();">
Share:
21,234
Arya Lisa
Author by

Arya Lisa

I am a entry level php developer

Updated on July 29, 2022

Comments

  • Arya Lisa
    Arya Lisa almost 2 years

    I had a image which calls add_cart() JavaScript function when onclick()

    <img src="images/add.png" onclick="add_cart()">
    

    I want the user to click the image only one time. When the user clicks it first time, add_cart function should be called. If he clicks second time no event should happen.