CHANTI
CHANTI

Reputation: 1475

Disable onClick functionality using JavaScript

I am using onClick event for the image.

When I am disable for the particular case, I didn't know exactly how can disable the onClick functionality using JavaScript.

Sample code:

<td>
    <a onClick="fun()">
        <html:img src="/caleder.jpg ... />
    </a>
</td>

calling fun:

function fun() {
    // in fun I want to disable the img and onClick functionality
    // i.e when I click that img, then it can't perform the onClick event.
}

Upvotes: 1

Views: 7009

Answers (4)

Omid Moghadas
Omid Moghadas

Reputation: 347

you can easy handle this issue with this code after you function see this codes

function disableClick() {
   document.getElementById('yourId you want disable on click').onclick = "";
}

and enable same this code

Upvotes: 1

user457015
user457015

Reputation: 981

The best way IMO.

function fun() 
{
    if (this.clicked == undefined)
    {
        this.clicked = true;
        // have fun
    }
    return false;
}

Upvotes: 0

user372743
user372743

Reputation:

The probably most efficient way since it removes the event listener:

<script>
  function fun(node) {
    document.getElementById(node).removeAttribute("onClick");
  }
</script>

Inside of the onClick() you need to place a this so it would be onClick(this)

Upvotes: 3

Brandon Frohbieter
Brandon Frohbieter

Reputation: 18139

I usually just set a variable and put the events in the onClick handler inside a conditional statement. Set the variable on the first click, and next time it won't fire.

<script>
  var i = 0;
  function clickHandle(){
    if(i==0){
       //do stuff
       i=1;
  }
<script>

Upvotes: 1

Related Questions