»
S
I
D
E
B
A
R
«
Switching Div Background Image Using jQuery
November 21st, 2009 by admin

To switch the background image using jQuery is easy.

If you have a div with id named ‘divID’, background image ‘myimage1.jpg’, and want to toggle between ‘myimage1.jpg’ and ‘myimage2.jpg’, here is what you can do:

var cur = 'first';
$('#divID').click(function() {
    if (cur == 'first') {
        $('#divID').css("background-image", "url(myimage2.jpg)");
        cur = 'second';
    } else {
        $('#divID').css("background-image", "url(myimage1.jpg)");
        cur = 'first';
    }
});
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay



Similar Posts:
Leave a Reply

Powered by WP Hashcash