Reputation: 119
I have an array of names and over 100 geographic coordinates. I am dynamically creating an appended HTML table of the 5 closest coordinates to a specified address. I want to do a conditional statement based on the selected address in the list of 5 closest addresses. I am able to pass the numeric values to a supporting function but the string value isn't being passed to the function, which is required for the conditional logic. if i plug in the value i as the parameter for showRouteB() then the function will work, however when i try to plug in city.name or variable assigned to city.name then the function doesn't seem to get called. i couldn't find any errors when i checked the console in Chrome and i've tried several different variations. does anyone see any obvious syntax or logic errors?
My code is:
for (i = 0; i < 5; i++) {
city = top101citiesUS[i];
//dynamically load map_table
tbody += '<tr>';
tbody += '<td>';
tbody += '<a href=#'+city.name+' onclick=showRouteB('+city.name+')>' +city.name+ '</a>';
//tbody += city.name + city.lat + ',' + city.lng;
tbody += '</a>';
tbody += '</td>'
tbody += '</tr>';
}
.....i then append string values for table header + table body + table footer
function showRouteB(x){
var element = document.getElementById('ProuteB');
element.innerHTML = "This is the city name " + x;
}
Upvotes: 1
Views: 5058
Reputation: 119
The answer from Levi helped me on my way however the resolution was that the array of values, such as Charles Street, Smith Ave, and Vanburen Lane contained spaces that required a %20. When the value was passed to the function then it was being interpreted as "showRouteB('Smith " and it created a logical syntax error since the "')" was missing. I had to remove the whitespace from my array of values in order to find the problem. Levi, thanks for the reply!
Upvotes: 0
Reputation: 25111
You need to add quotes around the strings, otherwise they are treated as variables.
tbody += '<a href=#'+city.name+' onclick=showRouteB("'+city.name+'")>' +city.name+ '</a>';
Upvotes: 2