v1r00z
v1r00z

Reputation: 796

jquery unbind function event once executed

Hi Folks, Please help me to fix this : Suppose we call the same javascript function from anchors but with different parameter

<a id="edit1" onclick="foo(1)"></a> 
<a id="edit2" onclick="foo(2)"></a>

function foo(id)
{
// let's say the code here hide the clicked anchor and show a new anchor with cancel-edit id 
}

The scenario :

i hope it's clear :X ! Thanks in advance.

Upvotes: 0

Views: 506

Answers (1)

user113716
user113716

Reputation: 322452

If you want to disable it for all the elements after one is clicked, you could just set a flag:

var enabled = true;
function foo(id) {
    if( enabled ) {
        // run your code
        enabled = false;
    }
}

It will continue to run, but the code in the if statement will not after the first click.

function stop() {

    // replace the original anchor

    enabled = true; // enable the "foo" handler

}

When the new anchor with stop() is clicked, you replace it with the original anchor, and set enabled to true so that foo() will work again.

Upvotes: 1

Related Questions