Skip to content Skip to sidebar Skip to footer

Changing Background Color Of A Div Based On Background Color Of Some Other Div

How to change the color of div with 'MyBullets' class with the color in a div with 'MyNumbers' Class in JQuery? somthing like. $MyComponent.find('.MyBullets').css('background-colo

Solution 1:

try this,

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

 </script>
  <script>
    function changeColor() {

        var color = $(".MyNumbers").css("background-color");
        $(".MyBullets").css("background-color", color);
    }



</script>

</head>
<body>

<div class="MyBullets">MyBullets</div>
<div class="MyNumbers" style="background-color:Red";>MyNumbers</div>
<input type="button" value="click here to change color" onclick="changeColor()" />
</html>

Solution 2:

var color = $(".MyNumbers").css("background-color");
    $(".MyBullets").css("background-color", color);

Solution 3:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    //Handle Click Event
    $("#btn").click(function()
    {
        var ColorCode= $(".MyNumbers").css('backgroundColor');
        $(".MyBullets").css('backgroundColor',ColorCode); 

    });

});
</script>
<style>
.wrapper { height:150px; width:500px; border:1px solid #e4e4e4; margin:auto; padding:10px;}
.MyNumbers { height:100px; width:240px; background:#06C; float:left;}
.MyBullets { height:100px; width:240px; background:#fff; float:right;}
.btn { margin:10px auto 10px; background:#484848; color:#FFF; height:35px; }
</style>
</head>

<body>
<div class="wrapper">
<div class="MyNumbers"></div>
<div class="MyBullets"></div>
<input type="button" id="btn" value="Change Color" class="btn" />
</div>
</body>
</html>

Post a Comment for "Changing Background Color Of A Div Based On Background Color Of Some Other Div"