views:

295

answers:

1

Hi Everyone,

I'm trying to dynamically change the background image of a div inside of a modal popup window. I first tried it with simplemodal and now I am trying it with the jqueryui dialog box. I can't get it to work on either one. Here is my code so far:

//Jquery Dialog Attempt:  
//I have also tried it in the open event
        $(".Previous tr td img:not(.Help)").live("click", function(){
            var mediumImage = $(this).parent().find("img.mediumImage");
            var smallImageDiv = $("div#modal table tr td div#smallerImage");
            var backgroundImageString = "url(\"" + mediumImage.attr("src").toString() + "\")";
            smallImageDiv.css('backgroundImage', backgroundImageString);
            $("div#modal").dialog({
                height:300,
                modal:true,
                close: function(){
                    $(this).dialog('destroy');
                }
            });
        });

//SimpleModal attempt
$(".Previous tr td img:not(.Help)").live("click", function(){
    var mediumImage = $(this).parent().find("img.mediumImage");
    var smallImageDiv = $("div#modal table tr td div#smallerImage");
    $("#modal").modal({
        onOpen: function(){
            var backgroundImageString = "url(\"" + mediumImage.attr("src").toString() + "\")";
            smallImageDiv.css('backgroundImage', backgroundImageString);
        },
        onShow: function(){
            $("html").css("overflow", "hidden");
        },
        onClose: function(){
            $("html").css("overflow", "auto");
            $(".Previous tr td img").live("click", function(){});
            $.modal.close();
        }
    });
});

EDIT: Based On Suggestion From ghoppe Apparently I'm having a strange encoding problem when setting the background image property.

Test:

//The path to the image is: Thumbnails\\2010\1\28\THUMBNAIL\0123456-0123a1of3_med.JPG
var backgroundImageString = "url(" + mediumImage.attr("src").toString() + ")";
smallImageDiv.css('background-image', backgroundImageString);
alert(backgroundImageString.toString()); //This is correct
alert(smallImageDiv.css('background-image').toString()); //Lots of escaped characters

Solution: I needed to escape my image path.

var backgroundImageString = "url(" + escape(mediumImage.attr("src")) + ")";
+1  A: 

smallImageDiv.css('backgroundImage', backgroundImageString); should be

smallImageDiv.css('background-image', backgroundImageString);
ghoppe
+1 to you, nice catch
marcgg
Still doesn't appear in the div, Thanks for the suggestion though!
Jon
Sorry I now realize as per http://api.jquery.com/css/, jquery understands the DOM formatting of multiple-word properties, so this wasn't your issue. Have you checked to see if the image comes up 404?
ghoppe
I updated the question with more information.
Jon
Have you tried dropping the `.toString()` altogether? .attr() will already contain a string.
ghoppe