I have a click event I wired up on a div on my page.
Once the click event has fired, I want to unbind the event on that div.
How can I do this? Can I unbind it in the click event handler itself?
I have a click event I wired up on a div on my page.
Once the click event has fired, I want to unbind the event on that div.
How can I do this? Can I unbind it in the click event handler itself?
Use the "one" function:
$("#only_once").one("click", function() {
alert('this only happens once');
});
There's the unbind function documented here:
http://docs.jquery.com/Events/unbind
Fits your example :)
In plain JavaScript:
var myDiv = document.getElementById("myDiv");
myDiv.addEventListener('click', clicked, false);
function clicked()
{
// Process event here...
myDiv.removeEventListener('click', clicked, false);
}
Steve