Satch3000
Satch3000

Reputation: 49364

Javascript Adding variable to code

This might be a really simple issue but I cannot spot the problem.

I have this code:

output += '<li><a href="#"><img src="'+value.thumbnail_url+'" /><h3>'+value.title+'</h3>'+value.body+'</a></li>';

I need to add this code after the href="#"

and replace the url string with this: value.media_url

I've come up with:

output += '<li><a href="#" onclick="window.plugins.childBrowser.showWebPage('+value.media_url+');"><img src="'+value.thumbnail_url+'" /><h3>'+value.title+'</h3>'+value.body+'</a></li>';

but there seems to be a syntax issue with the above as the link is not working.

The broken code is somewhere here: onclick="window.plugins.childBrowser.showWebPage('+value.media_url+');" as the rest works fine.

I could go even further ... here:

('+value.media_url+')

Can anyone see the problem?

Upvotes: 1

Views: 74

Answers (2)

Rab
Rab

Reputation: 35572

replacing onclick="window.plugins.childBrowser.showWebPage('+value.media_url+');" with onclick="window.plugins.childBrowser.showWebPage('"+value.media_url+"');" will fix it

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

The resulting string is:

window.plugins.childBrowser.showWebPage(my/url/to/file.png)

As you can see, it's missing quotes around the string. Since it's in an attribute, you need this:

... onclick="window.plu.....WebPage(&quot;'+value.media_url+'&quot;)" ...

Upvotes: 2

Related Questions