jQuery Style How to - Check element color








Question

We would like to know how to check element color.

Answer


<!--  w  w  w .  j  av  a2  s  .  co  m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<style type='text/css'>
h1 {
  color: red;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $(function () {
        if ($('h1').css('color') == 'rgb(255, 0, 0)') {
            $('h1').css('color', 'blue');
        }
    });
});
</script>
</head>
<body>
  <h1>My color is Red</h1>
</body>
</html>

The code above is rendered as follows: