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?
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() .
Syntax: $(". event-handler-btn"). one("click", function (e) { // Code to be executed 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.
Check out the one event. Documented example:
$('#foo').one('click', function() {
alert('This will be displayed only once.');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With