tags:

views:

62

answers:

3

Hi all!

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;
});
A: 
var borderColor = $(this).css("border-color")

You need to show more than this. But if this is all you have then the problem is that this isn't defined.

var borderColor = $('.divIm').css("border-color");

Will be what you need.

Galen
$("div.divIm").mouseover(function() { var borderColor = $(".divIm").css("border-color"); debugger;});I've just tried as you suggested but the problem wasn't solved. $(this) returns a correct object. May be there is another way...
Desperadeus
In that case you can use this inside the function. try using borderColor instead of border-color as Gert suggested
Galen
It didn't help. Dave has given right answer.
Desperadeus
+2  A: 

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

Sarfraz
Thanks for your answer; using the plugin seems to be too heavy for the issue. Though you may be right.
Desperadeus
jQuery returns the computed style if the requested style property isn't set on the element. It uses *$.curCSS(elem, style, force)*, which is an undocumented method.
Andy E
+7  A: 

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.

Dave
Thanks for the advice, it helps!
Desperadeus