Hi all, I wonder if somebody can help me with this. I have this situation:
I have a header div with a heading and when I click on it another div hiddenDiv slides down or slides up (I am using the slideToggle method). What I want to do also is to change the background image on the header div, depending on whether the hiddenDiv is collapsed or not. Let's have a look at the code:
HTML
<div class="wrapper">
<div class="header"></div>
<div class="hiddenDiv">
<p>I am displayed/hidden when user clicks on header</p>
</div>
</div>
SCRIPT
$(document).ready(function(){
$(".header").click(function(){
$(this).siblings("hiddenDiv").slideToggle("fast");
});
});
Assuming the header div needs to have a "+" background image when the hidden div is collapsed and a "-" when it is open, what would be the best ay to implement this with the slideToggle method?
I was thinking - but I am open to suggestions of course - to have two more classes:
.plus{
background: #fffff url("plusSign.jgp") no-repeat 100% 0;
}
.minus{
background: #fffff url("minusSign.jgp") no-repeat 100% 0;
}
and then add them in the script like this:
$(document).ready(function(){
$(".header").click(function(){
var $thisHiddenDiv = $(this).siblings("hiddenDiv")
$thisHiddenDiv.slideToggle("fast");
if($thisHiddenDiv:visible){
$(this).removeClass("plus").addClass("minus");
}
else{
$(this).removeClass("minus").addClass("plus");
}
});
});
Would that work? Unfortunately I don't have any code to try it on right now, the above is just a demonstration.
cheers