Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Parameter Passing Not Working

The code dynamically creates a listview which works but i want to make it so when a listview item is clicked it sends the a url paramater to another method. When i set a paramater it doesnt alert the paramater, but when i give no parameter it works.

var output = 
"<li onclick='openURL()'><h3> Module Code: " + 
results.rows.item(i).module 
+ "</h3>Room: " 
+ results.rows.item(i).room +
"</li>";

The above works - No parameter in openURL();

var output = 
    "<li onclick='openURL('" + results.rows.item(i).url + "')'><h3> Module Code: " + 
    results.rows.item(i).module 
    + "</h3>Room: " 
    + results.rows.item(i).room +
    "</li>";

The above doesnt work - I have done alert(results.rows.item(i).url) and it has a value.

function openURL(url) {
    alert("opening url " + url);
} 

Could someone explain what i'm doing wrong, i've been trying to solve the problem for hours.

Cheers!

like image 378
SkelDave Avatar asked Sep 18 '26 05:09

SkelDave


1 Answers

You are using single quotes to open the HTML attribute, you can't use it as JavaScript String because you'll be closing the HTML attribute, use double quotes:

var output = 
    "<li onclick='openURL(\"" + results.rows.item(i).url + "\")'><h3> Module Code: " + 
    results.rows.item(i).module 
    + "</h3>Room: " 
    + results.rows.item(i).room +
    "</li>";
like image 132
José Roberto Araújo Júnior Avatar answered Sep 20 '26 17:09

José Roberto Araújo Júnior