Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get border color with jQuery in IE with .css()

There is a problem getting border color of the clicked element in Internet Explorer.

$("#clickme").click(function() {
    alert($(this).css('border-color'));
});

Here is jsfiddle: http://jsfiddle.net/dS7mc/
It works in chrome but it doesn't work in IE10.

Any ideas how to make it work with both? Also changing to "border" only, in chrome it gives me 2px solid rgb(0, 0, 0) but in Ie i still get empty alert.

PS. Yes, I've tried "borderColor". Also doesn't work in IE

like image 557
Kedor Avatar asked Mar 28 '13 20:03

Kedor


4 Answers

This is because in Internet Explorer there is no border-color. The property gets renamed to border-pos-color:

border-top-color: #000000;
border-right-color: #000000;
border-bottom-color: #000000;
border-left-color: #000000;

The same applies to border-width and border-style (border-left-width, etc.). In order to pull the border color (assuming all 4 are the same), you'd use:

$(this).css('border-top-color');

Equally to pull the border-width or border-style (again assuming all 4 are equal) you'd use:

$(this).css('border-top-width');
$(this).css('border-top-style');

You can find which style properties an element has with IE's F12 developer tools:

IE Dev Tools

like image 133
James Donnelly Avatar answered Nov 14 '22 12:11

James Donnelly


Try this ..Works on IE8

$("#clickme").click(function() {
    $('body').append($(this).css('border-top-color'));
});

enter image description here

jsFiddle

Also, Colors are returned by browsers in a different way FireFox, Safari and Chrome return them as rgb() values and IE returns them exactly like you set them in your CSS even when you use the shorthand notation (#f00 vs #ff0000) and Opera always returns the color as hexidecimal with 6 digits

like image 5
Mohammad Adil Avatar answered Nov 14 '22 13:11

Mohammad Adil


similar to Mohammad Adil:

$("#clickme").click(function() {
    var Bcolor = $(this).css("border-left-color");
    alert(Bcolor);
});

you have to spec each side

like image 2
kelly johnson Avatar answered Nov 14 '22 12:11

kelly johnson


try regular js

var clickme = $("#clickme")[0];
    clickme.addEventListener('click', function() {
    alert(clickme.style.borderColor)
    }, false)
like image 1
James Daly Avatar answered Nov 14 '22 14:11

James Daly