jquery execute event only once

avnish
avnish
Participant
242 Points
11 Posts

pls help me with example on how to run jQuery only one time.

Views: 9238
Total Answered: 1
Total Marked As Answer: 1
Posted On: 14-May-2015 03:16

Share:   fb twitter linkedin
Answers
Brian
Brian
Moderator
2232 Points
14 Posts
         

Hi Avnish,

Use JQuery .one() or .off() for fire event onnly once.

$("#nice").one("click", function () {
alert("This will be displayed only once.");
});

 

$("#nice").on("click", function (event) {
alert("This will be displayed only once.");
$(this).off(event);
});

 

In both example a click on the element with ID nice will display the alert once. Subsequent clicks will do nothing.

Posted On: 16-May-2015 21:48
 Log In to Chat