views:

78

answers:

7

Can anybody know how to get border-color of a div using jquery.

Jquery:

$("#divcolor").click(function (){
  alert("dsf");
  var divcolor = $(this).css("border-color");
  alert(divcolor);
});

Html:

<div id="divcolor" style="border:#333333 solid 1px;" >
This is the target
</div>

In divcolor variable i am not getting anything.

+3  A: 

Using the CSS jQuery function like you did:

http://docs.jquery.com/CSS/css#name

But read this paragraph:

Shorthand CSS properties (e.g. margin, background, border) are not supported. For example, if you want to retrieve the rendered margin, use: $(elem).css('marginTop') and $(elem).css('marginRight'), and so on.

Mike Gleason jr Couturier
@Mike background-color is coming but border-color is not coming.
Srikanth
+1 for finding the documentation.
Kobi
Why was this answer chosen? It seems it doesn't answer the question.
jpartogi
+1  A: 

Your mistake is elsewhere. That code works on Chrome and IE.

Yuriy Faktorovich
A: 

border-color isn't working for me either (on Firefox), but this works:

$(this).css("border-top-color")

http://jsbin.com/ezefu

Kobi
@Kobi: I was curious to know how JQuery would get the color for different sides of the border, your answer clears this a bit :). Thanks
Mahesh Velaga
A: 

Try this one:

Jquery:

$("#divcolor").click(function (){
  alert("dsf");
  var divcolor = $(this).css("border-color");
  alert(divcolor);
});

Html:

<div id="divcolor" style="border:1px solid; border-color:#333333;" >
This is the target
</div>
Sarfraz
A: 
$("#divcolor").click(function (){
      var divcolor = $(this).css("borderColor");
      alert(divcolor);
});
jpartogi
Good idea, though it gives me the four values: `rgb(51, 51, 51) rgb(51, 51, 51) rgb(51, 51, 51) rgb(51, 51, 51)`
Kobi
Well, you can get the hex value by saying parseInt((a*256*256)+(b*256)+c, 16) ;-)
James Wiseman
A: 

I always consider it better practice to work with CSS classes instead of CSS direct. Then you could have something like:

$(this).hasClass("MyClassWithTheBorderColorStyleInIt");
James Wiseman
A: 

else you can write like this

        $("#divcolor").click(function()
        {
            var divcolor = $(this).css("border");
            divcolor = divcolor.substring((divcolor.indexOf(' ') + 1), divcolor.length);
            divcolor = divcolor.substring((divcolor.indexOf(' ') + 1), divcolor.length);

            alert(divcolor);
        });
Devi
But this will be applicable only when you are writing shorthand notations. The best practice is to use different class, as said earlier :)
Devi