Pepe
Pepe

Reputation: 61

How to get background color of div?

I have a div I want when user clicks on this div the background color of div should be display in alert box using jquery only ?

Upvotes: 4

Views: 13374

Answers (4)

gaurang171
gaurang171

Reputation: 9080

#divBack{
  background-color:#00FFCC;
}
<div id="divBack">click to Get my background</div>
$('#divBack').click(function() {
    alert($(this).css('background-color'));
});

I have created bin for you please check this link http://codebins.com/codes/home/4ldqpc1

Upvotes: 0

Tadeck
Tadeck

Reputation: 137360

You can get the color like this:

$('div#test').css('background-color'); // returns RGB

and display it after click like that:

$('div#test').click(function(){
    alert($(this).css('background-color'));
});

See fiddle: http://jsfiddle.net/xWkDW/1/

Upvotes: 1

alex
alex

Reputation: 490273

$('div').click(function() {
   alert($(this).css('background-color'));
});

jsFiddle.

Upvotes: 6

Amit
Amit

Reputation: 22076

Try this

<script type="text/javascript">
$(document).ready(function(){
  $("div").click(function(){
    var $c=$(this).css("background-color");
    alert($c);
  });
});
</script>

Upvotes: 9

Related Questions