How to get div background color?

I have a div I want, when the user clicks on this div, the background color of the div should only be displayed in the warning field using jquery?

+4
source share
4 answers

try it

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

jsFiddle .

+6
source

You can get the color as follows:

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

and display it after clicking as follows:

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

See the script: http://jsfiddle.net/xWkDW/1/

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

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

0
source

All Articles