tags:

views:

57

answers:

2

Hey guys, I'm trying to change the background image of multiple divs with the class .innerpreview when a drop down selection is made. Any idea why the following isn't working?

$('#txtMontage').change(function(event) {
    if (this.value == "example") {
        $('.innerpreview').css('background-image', 'img/img-bkg.jpg)');
    }
});

Thanks in advance.

+4  A: 

Missing url( ... thanks Ender:

$('#txtMontage').change(function(event) {
    if (this.value == "example") {
        $('.innerpreview').css('background-image', 'url(img/img-bkg.jpg)');
    }
});

I would also not use this, but create a new class in CSS with the background image specified, and change or add this new class.

Martin
+1, separation of style and behavior is good practice.
You
+2  A: 

I think the problem is that you are missing the "url()" bit of setting the background image. You should have something like this:

$('#txtMontage').change(function(event) {
    if (this.value == "example") {
        $('.innerpreview').css('background-image', 'url(img/img-bkg.jpg)');
    }
});

See this question: Switching DIV Background Image With jQuery

Ender
+1 OP needs `url(img/img-bkg.jpg)`.
patrick dw
Thanks Ender, missed that. But I stand behind my statement to use a CSS class for this.
Martin