Soatl
Soatl

Reputation: 10592

Javascript Error: 'missing ) after argument list"

I am making an image for my webpage through javascript like so:

photoHTMLString = '<li class = "SliderPhoto"><img src =  "' + ImageArray[x].src_small + '" size = "thumb" onclick = "ShowImagePopUP(' + ImageArray[x].src_big + ')" class = "FacebookSliderPhoto"/></li>';

Whenever I try and click a photo go into ShowImagePopUP I get this error:

missing ) after argument list
[Break On This Error] ShowImagePopUp(http://a8.sph...389_84095143389_5917147_2636303_n.jpg)

It doesn't look like I am missing any ')'s so I am lost on the error. Any suggestions?

Upvotes: 9

Views: 56076

Answers (4)

Eineki
Eineki

Reputation: 14909

try

photoHTMLString = '<li class = "SliderPhoto"><img src =  "' 
                + ImageArray[x].src_small 
                + '" size = "thumb" onclick = "ShowImagePopUP(\"' 
                + ImageArray[x].src_big + '\")" class = "FacebookSliderPhoto"/></li>';

should do the trick and solve your problem leaving intact the uglyness of you code

A function like this one should be a bit readable and ready to use...

function slideElement(image){
    var li=document.createElement('li');
    var img=document.createElement('img');
    li.appendChild(img);
    li.setAttribute('class','SliderPhoto');
    img.setAttribute('class','FacebookSliderPhoto');
    img.setAttribute('size', 'thumb');
    img.setAttribute('src', image.src_small);
    img.setAttribute('onclick', function(){showImagePopUP(image.src_big);});

    return li;
}

Upvotes: 2

mellamokb
mellamokb

Reputation: 56769

You need to wrap the contents of ShowImagePopUP in quotes:

"ShowImagePopUp(\'' + ImageArray[x].src_big + '\')"

Which should render as:

ShowImagePopUp('http://a8.sph...389_84095143389_5917147_2636303_n.jpg')
               ^ note the quote here

Example: http://jsfiddle.net/V23J6/1/

Upvotes: 27

Tod
Tod

Reputation: 8242

Here's some general advice, build up the strings into intermediate variables and then assemble it at the end. You can then use the debugger to find out where you're getting your ' or "s unbalanced. When you have it all built you can coalesce it into a single line if you want or leave it with the intermediate variables.

Upvotes: 0

Quentin
Quentin

Reputation: 943214

The value in ImageArray[x].src_big needs to be quoted.

Try to avoid building HTML by mashing strings together. Using a DOM builder gives code that is much easier to debug.

You'd probably be better off writing this so the function computes the large URI based on the small URI rather than having it hard coded.

Upvotes: 0

Related Questions