0

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.

Carson
  • 4,541
  • 9
  • 39
  • 45

2 Answers2

4

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
  • 11,031
  • 8
  • 50
  • 77
2

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

Community
  • 1
  • 1
Ender
  • 14,995
  • 8
  • 36
  • 51