Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.css() doestn't return border-color which is set to red

Tags:

jquery

css

CSS: .divIm { border:1px solid red; } , and code-line var borderColor = $(this).css("border-color") returns "". What is wrong? Or would it be correct trying to get computed style if I use jQuery?

Update: Below is a code which doesn't want to work as it expected.

$("div.divIm").mouseover(function() {
  var borderColor = $(this).css("border-color");
  debugger;
});
like image 386
despero Avatar asked Aug 21 '10 20:08

despero


People also ask

Why is my border not working CSS?

If you've set the shorthand border property in CSS and the border is not showing, the most likely issue is that you did not define the border style. While the border-width and border-color property values can be omitted, the border-style property must be defined. Otherwise, it will not render.

What is a correct syntax to add border style and Colour?

Each side can be set individually using border-top-color , border-right-color , border-bottom-color , and border-left-color ; or using the writing mode-aware border-block-start-color , border-block-end-color , border-inline-start-color , and border-inline-end-color .


2 Answers

Since every of the four borders can have a different color, .css('border-color') cannot work out, which color to return (even if they are all the same).

In most of the cases, the color of all borders is the same, so you can do it like this:

$('div.divIm').mouseover(function() {
    var borderColor = $(this).css('border-left-color');
    debugger;
});

So you get the color of the left border and that should be enough for your needs.

like image 168
davehauser Avatar answered Sep 23 '22 13:09

davehauser


You can get the computed style with curStyles jQuery Plugin including multiple computed styles.

like image 33
Sarfraz Avatar answered Sep 22 '22 13:09

Sarfraz