jQuery Style How to - Add a CSS property if a CSS color is present








Question

We would like to know how to add a CSS property if a CSS color is present.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<style type='text/css'>
#div1 {<!--from ww  w.ja va 2  s .  c  om-->
  color: red;
}
</style>
<script type='text/javascript'>
$(function(){
    console.log($('#div1').css('color'));
    console.log($('#div1').css('color') == 'rgb(255, 0, 0)');
    if ($('#div1').css('color') == 'rgb(255, 0, 0)') {  
        $("#div1").css("color","blue");
    }
});
</script>
</head>
<body>
  <div id="div1">Test</div>
</body>
</html>

The code above is rendered as follows: