0
<p class="pen" style="color:#000">abc</p>

.pen{
color:#333
}

In the above html and css, the inline style has greater precedence than the class, thus #000 will be used. But when I do $('.pen').css('color'), I get #333. How can I get the color of the current style?

10
  • This is unexpected behavior. Can you provide additional code/markup? Commented Apr 3, 2015 at 2:40
  • @WillReese what is unexpected? I guess I have given quite a lots of detail. Commented Apr 3, 2015 at 2:41
  • Your jquery should return #000 Commented Apr 3, 2015 at 2:41
  • 1
    OP said :: "But when I do $('.pen').css('color'), I will got the #333. " that is incorrect. the output I got on my fiddle says otherwise. So I am still trying to understand how OP got the value #333 or its equivalent as his output to the command Commented Apr 3, 2015 at 2:49
  • 1
    Unless OP can reproduce the issue, I vote to close this as off-topic. Commented Apr 3, 2015 at 2:54

1 Answer 1

2

If you have two elements with the same class

<p class="pen" style="">abc</p>
<p class="pen" style="color:#000">abc</p>

and run,

$('.pen').css('color')

The output would b #333 or rgb(51,51,51).; because the selector finds the first element matching. So this is the problem with your code - Multiple elements with class pen.

Fiddle


In case of single pen element or the order

<p class="pen" style="color:#000">abc</p>
<p class="pen" style="">abc</p>

the output is #000.

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.