Hesus Ko
Hesus Ko

Reputation: 47

How do I get the background-color value?

<input class="color" value="66ff00" id="fc" autocomplete="off" style="background-image: none; background-color: rgb(128, 225, 255); color: rgb(0, 0, 0);">

How can I get the value of the background-color?

Everytime I use (element).attr("background-color"), it doesn't work. I suppose it has to do with style="background-color".

Upvotes: 0

Views: 124

Answers (8)

Surama Hotta
Surama Hotta

Reputation: 130

We can use .css property to get specific css value using id or class selector.

$("#fc").css('background-color') // fc is the id for the control

or

$(".color").css('background-color') //color is the class applied here

Upvotes: 0

Jed La Rosa
Jed La Rosa

Reputation: 57

Try this:

$("input.color").css("background-color"); 

It will return you the specified background-color.

and then do this to get the result:

var color = $("input.color").css("background-color"); 

$("$target_element").css("background-color", color);

Upvotes: 0

It will work you will get the backround color of #fc

var fc = document.getElementById('fc');
var bgcolor = fc.style.backgroundColor;

Upvotes: 0

Choco
Choco

Reputation: 1064

Use this it will work..

<script>
$( document ).ready(function() {
alert($("#fc").css('background-color'));
});
</script>

<input class="color" value="66ff00" id="fc" autocomplete="off" style="background-image: none; background-color: rgb(128, 225, 255); color: rgb(0, 0, 0);">

Upvotes: 0

Divakar Gujjala
Divakar Gujjala

Reputation: 803

If you want to get any css property in jQuery then use

$(selector).css(property);

Here, in this case it is $("#fc").css('background-color');

Upvotes: 0

Terry
Terry

Reputation: 66103

Use the .css method to fetch the background colour, ie:

$(element).css('background-color');

You may also access it without quoting, using the object:

$(element).css(bgColor);

Upvotes: 0

Hatjhie
Hatjhie

Reputation: 1365

Try:

(element).css("background-color");

Hope it helps.

Thanks

Upvotes: 0

Vladimirs
Vladimirs

Reputation: 8599

You can access any css property via css:

$(".color").css('background-color')

Jsfiddle

Upvotes: 2

Related Questions