Monday, March 23, 2015

Attach an event to element to execute only once.

Today I have been using JQuery and confront an  interesting situation to click once on a Div. .To do this use One function exist into JQuery .

Generally when we attach an event to any element, event functionality remains with the element till element gets removed .If we delete that elements means that functionality has been deleted but if you don’t want to remove that element and want to run the event functionality only once,Kindly use this approach.

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.

  1: $(document).ready(function() {
  2:     $("#DotnetPiper").one("click", function() {
  3:         alert("You have click once on");
  4:     });
  5: });​

After the code is executed, a click on the element with ID dotnetpiper will display the alert. Subsequent clicks will do nothing.

This is best in use when you want to execute functionality once.


To know more MVC and WebApi Kindly go through with these links

MVC Articles & WCF and WebApi




Post a Comment