Oto Shavadze
Oto Shavadze

Reputation: 42773

possible clear jquery `one` method

Its possible to clear jquery one property? for example given html

    <div id="button">button</div>

    <div id="clearOneProperty">clear one property</div>

and js

$("#button").one("click", function () {
    alert("blah");
});

$("#clearOneProperty").on("click", function () {
    // clear "one" property, that is after this event,  I want "#button"  to work again
});

Demo http://jsfiddle.net/RzzCu/2/

So, if click #button div, alert happened only one times right?

And I want that, at click on #clearOneProperty, reset one property. Its possible?

P.S. I am not asking how to make this with other ways, I am interest exact: "possible clear jquery one method?". Thanks.

Upvotes: 5

Views: 6248

Answers (5)

Adil
Adil

Reputation: 148130

The one unbinds its self on first invocation as stated "The first form of this method is identical to .bind(), except that the handler is unbound after its first invocation", jQuery Documentation *So you need to bind it again*.

Live Demo

$("#button").one("click", onefunction);

function onefunction() {
    alert("blah");
}
$("#clearOneProperty").one("click", function() {       
    $("#button").one("click", onefunction);
});​

Upvotes: 3

bonbonez
bonbonez

Reputation: 6848

Try

$('#button').unbind('click');

Upvotes: 0

Dineshkani
Dineshkani

Reputation: 3015

Use unbind() method

$("#button").unbind();

Upvotes: 0

Th0rndike
Th0rndike

Reputation: 3436

Just rebind it inside the function.

$("#button").one("click", function () {
    alert("blah");
});

$("#clearOneProperty").one("click", function () {
    $('#button').one("click", function () {
    alert("blah");
});
});

here a fiddle

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337580

Try this:

function bindButton() {
    $("#button").unbind('click').one("click", function() {
        alert("blah");
    });
}

bindButton();

$("#clearOneProperty").on("click", function() {
    bindButton();
});

Updated fiddle

Upvotes: 7

Related Questions