3
I have the following problem, I made navigation arrows for some elements and depending on the active element, I need to change the CSS of another element.
In the example below I would need to change for example the background of the body to a specific color, every time the class teste2 receive the class Current. I tried to do with an on change in the document but did not succeed.
var $divs = $('.box').children(".teste"),
index = 0;
$("#next").click(function() {
updateStatus(1);
});
$("#prev").click(function() {
updateStatus(-1);
});
function updateStatus(a) {
$divs.eq(index).removeClass("current").hide();
index += a;
$divs.eq(index).addClass("current").show();
$("#next").toggle((index !== $divs.length - 1));
$("#prev").toggle(index !== 0);
}
.box{
height:100px;
width:100px;
outline:1px solid red;
}
.teste{
height:50px;
width:50px;
}
.teste1{
background:#ccc;
}
.teste2{
display:none;
background:red;
}
.teste3{
display:none;
background:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="prev">Prev</div>
<div id="next">Next</div>
<div class="box">
<div class="teste teste1"></div>
<div class="teste teste2"></div>
<div class="teste teste3"></div>
</div>
I don’t fully understand, but if the Current exchange happens in addClass, Voce could do an if testing if the index is = 2 and if yes apply css via jquery $(body). css("background-color": "#ddd");
– Cleverson
@Cleverson, that’s just what I needed
– haykou