[jquery] how to get the value of css style using jquery

I'd like to know what type of method should I use to get the value of CSS style. I want to set it to jQuery so that I can create conditions to match on CSS "left" value.

Here is the CSS and HTML tag with the style attributes.

<div class="items" style="left: -900px"> <span>some content here</span> </div>

Here is my jQuery code to get the value.

<script>
  var n = $("items").css("left");
  if(n == -900){
    $(".items span").fadeOut("slow");
  }
</script>

I'd like to know if this method is correct because it is not working in my end. Thanks in advance for the little help.

Note: left value was dynamic, and it was changing to these value: -900px, -1800px, -2700px, -3600px, -4500px...

This question is related to jquery css

The answer is


Yes, you're right. With the css() method you can retrieve the desired css value stored in the DOM. You can read more about this at: http://api.jquery.com/css/

But if you want to get its position you can check offset() and position() methods to get it's position.


You code is correct. replace items with .items as below

<script>
  var n = $(".items").css("left");
  if(n == -900){
    $(".items span").fadeOut("slow");
  }
</script>