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.”);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.