tags:

views:

172

answers:

2

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 dont detect it again and add the CSS style.

This is class is applied when certain elements are hovered over.

Is this possible in jQuery?

+9  A: 

Check out the one event. Documented example:

$('#foo').one('click', function() {
  alert('This will be displayed only once.');
});
karim79
I don't think "one" will solve danits problem. Can one be bound to document ready?
hunter
@Hunter - The op said 'This is class is applied when certain elements are hovered over.' which led me to believe that he wants a mouseover event to fire just once on certain elements. I could be wrong, of course, in which case your answer is probably the way to go.
karim79
+1  A: 

This will fire once on page load

$(function(){ 
    if ($("#elementid").hasClass("active"))
    {
        $("#elementid").css("margin-top", "4px");
    }
});
hunter
I don't think "one" is necessary in this case. Using this solution will work --- document.ready fires only once.
macca1