I have a simple jQuery click handler that animates the change of a divs background image. I would like to create some reusable code so I would not have to copy my code for every div I would like this effect on. How would I go about this?
<style type="text/css">
#divtest.toggled {
background-image: url('images/alert_closed_small.gif');
background-position: center;
background-repeat: repeat;
background-color:silver;
}
.nottoggled {
background-image: url('images/alert_future_small.gif');
background-position: center;
background-repeat: repeat;
background-color:silver;
}
<script type="text/javascript">
$(document).ready(function(){
var toggle=0;
$("#divtest").click(function(event){
if (toggle == 0) {
$("#divtest").slideUp("slow", function(){
$("#divtest").addClass("toggled");
});
$("#divtest").slideDown("slow", function() {
toggle=1;
});
}else if(toggle == 1) {
$("#divtest").slideUp("slow", function(){
$("#divtest").removeClass("toggled");
$("#divtest").addClass("nottoggled");
});
$("#divtest").slideDown("slow", function() {
toggle=0;
});
}
});