Tuesday, November 26, 2013

How to attach event to element which execute only once?

Usually when we attach event to element, event functionality remains with the element till element gets removed but if we do not want to remove the element and want to run the event functionality only once. What to do ?


Answer is jQuery one() method.

This attaches a handler to an event for the element. The handler is executed at most once per element. In simple terms, the attached function will be called only once.

$(document).ready(function() {
    $("#foo").one("click", function() {
        alert("This will be displayed only once.");
    });
});​
Related Posts Plugin for WordPress, Blogger...

Receive all updates via Facebook. Just Click the Like Button Below

You can also receive Free Email Updates: