I wish to avoid using css transitions.
I have three divs of the same class. I’m attempting to toggle the background color of one div when clicked. When the div is clicked again, or one of the other are clicked, I wish it to return to default.
I have included the jQuery color plugin and succeded in changing the color on click. I belive that my conditions are wrong and thats why i cant change the color back when clicking the other div.
This is what i’ve done:
<script type="text/javascript">
jQuery(document).ready(function() {
$('.link').click(function() {
var $me = $(this);
if ($me.css("background-color") == "#993") {
$me.animate({backgroundColor:"#246"}, 500);
} else {
$(".link").each(function() {
if ($(this) != $me) {
if ($(this).css("background-color") == "#246") {
$(this).animate({backgroundColor:"#993"}, 500);
}
}
});
$me.animate({backgroundColor:"#246"}, 500);
}
});
});
</script>
<style type="text/css">
.link {
width:100px;
padding:10px;
background-color:#993;
float:left;
display:inline;
text-align:center;
}
</style>
</head>
<body>
<div class="link">link</div>
<div class="link">link2</div>
<div class="link">link3</div>
I have had some trouble understanding the backgroundColor consept and hope for help. 🙂
Thanks!
Try this. CSS3 could help if you want to use css as well.
There are 3 divs and when you click one of them, it will become blue. When you click other divs, blue is gonna red and clicked one is gonna blue. If you gonna click blue, It will become blue back.