Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get background color of div?

Tags:

html

jquery

css

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 ?

like image 649
Pepe Avatar asked Dec 05 '22 22:12

Pepe


2 Answers

Try this

<script type="text/javascript">
$(document).ready(function(){
  $("div").click(function(){
    var $c=$(this).css("background-color");
    alert($c);
  });
});
</script>
like image 75
jams Avatar answered Dec 08 '22 02:12

jams


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

jsFiddle.

like image 40
alex Avatar answered Dec 08 '22 01:12

alex