Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Do only once

Tags:

jquery

I want to check if an element has the class .active, if it does add: margin-top: 4px;

However I only want this to occur once, when the page loads, once the class has been detected I don't want to detect it again and add the CSS style.

This class is applied when certain elements are hovered over.

Is this possible in jQuery?

like image 628
CLiown Avatar asked Mar 29 '10 15:03

CLiown


People also ask

What is jQuery once?

The . once() function takes one or two parameters: if there's just one and if it's a string, it filters out all elements which already have the processing function with that label run. If there is a second parameter, it runs that function for each matched elements that has not yet been processed, similar to . each() .

How do you attach a event to element which should be executed only once jQuery?

Syntax: $(". event-handler-btn"). one("click", function (e) { // Code to be executed once });

How do you attach a event to element which should be executed only once?

one() method , the code that attached to only gets executed once per page load. The jQuery one() method adds event handlers to the selected element. The handler is executed at most once per element per event type.


1 Answers

Check out the one event. Documented example:

$('#foo').one('click', function() {
  alert('This will be displayed only once.');
});
like image 120
karim79 Avatar answered Oct 22 '22 07:10

karim79