I personally would just use the JS to switch between 2 classes.
Have the CSS outlining everything you need on your div MINUS the background rule, then add 2 classes (e.g: expanded & collapsed) as rules each with the correct background image (or background position if using a sprite).
CSS with different images
.div {
/* button size etc properties */
}
.expanded {background: url(img/x.gif) no-repeat left top;}
.collapsed {background: url(img/y.gif) no-repeat left top;}
or CSS with image sprite
.div {
background: url(img/sprite.gif) no-repeat left top;
/* Other styles */
}
.expanded {background-position: left bottom;}
Then...
JS with images
$(function){
$('#button').click(function(){
if($(this).hasClass('expanded'))
{
$(this).addClass('collapsed').removeClass('expanded');
}
else
{
$(this).addClass('expanded').removeClass('collapsed');
}
});
}
JS with sprite
Note: the elegant toggleClass does not work in IE6, but the below addClass/removeClass method will work fine in this situation as well
most elegant solution (not IE6 friendly unfortunately)
$(function){
$('#button').click(function(){
$(this).toggleClass('expanded');
});
}
$(function){
$('#button').click(function(){
if($(this).hasClass('expanded'))
{
$(this).removeClass('expanded');
}
else
{
$(this).addClass('expanded');
}
});
}
As far as i know this method will work accross browsers, and i would feel much more comfortable playing with CSS and classes than with url changes in the script.
Cheers!
Happy coding:)
Kelly