Bharthan
Bharthan

Reputation: 1532

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-color",$thisComp.find(".MyNumbers").css(background-color)....);

Upvotes: 1

Views: 451

Answers (3)

vivek salve
vivek salve

Reputation: 991

<!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>

Upvotes: 0

Abhisek Mishra
Abhisek Mishra

Reputation: 269

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

Upvotes: 0

Anoop Joshi P
Anoop Joshi P

Reputation: 25537

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>

Upvotes: 3

Related Questions