0

I wonder whether there is a way to read a special css-value of a class with jQuery. The scenario looks more or less like this:

HTML:

<div class="box"></div>

CSS:

.box { margin-top: 100px; }

Now I would like to check whether that .box has a margin-top of 100px or not to make in if-statement based on it.

2
  • 3
    You can't actually read the value from the .box rule, but you can use .css('margin-top') to get the element's current computed margin-top value. Commented Mar 20, 2014 at 18:46
  • thank you, that's interesting! Commented Mar 20, 2014 at 19:12

1 Answer 1

1

The answer is exactly as Jason referred in the his comment. Here is a jsfiddle for your reference

jquery used:

$(function(){
    if($(".box").css('margin-top') == '100px'){
        alert('yes');
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah thank you! Also for the fiddle! But I still can't get working the thing I want to. I wanted to use this in an if-statement to check if a special action (reducing the margin-top of a special div) is allowed to be fired. My problem is: I have a prev/next navigation and when there is no "prev" or "next" element, the margin-top has to be animated to 0. Everything works fine, but when you double-click when this action has to take place the margin-top value gets two times the negative value... Have a look at it on line 62 following: jsfiddle.net/mleyendecker/632Fw/156
@MarcL good.. i have also created a fiddle jsfiddle.net/39james/eCUR9. If you want you can take a look at this. Basically what i did is when the navigation reaches the last item, i make it visible:hidden. My styles are not correct, so check the JQuery section and try adding it to your fiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.