Harshit Tailor
Harshit Tailor

Reputation: 3281

Put single quotes in string javascript

Currenly i use javascript to bind a table dynamically..

I need single quotes to delimit a string..

Code :-

var str ="";
str += "<td><a onclick='showFlightsByName("+val.Name+")' href='javascript:;'><span>" + val.Name + "</span></a></td>";

Current Ouput :-

<a onclick="showFlightsByName(AirTran Airways)" href="javascript:;"><span>Airways Airways</span></a>

Desire Output :-

<a onclick="showFlightsByName('AirTran Airways')" href="javascript:;"><span>Airways Airways</span></a>

Upvotes: 1

Views: 139

Answers (3)

azz
azz

Reputation: 5930

Often when working with HTML it's easier to use ' instead of " for Strings, like this:

var str = '<td><a onclick="showFlightsByName("' + val.Name + '")" href="javascript:;"><span>' + val.Name + '</span></a></td>';

However, have you considered using JS to build the DOM object?

var a = document.createElement("a");
a.href = "javascript:;";
a.onclick = "showFlightsByName(" + val.Name + ")";
a.appendChild(document.createElement("span").innerHTML(val.name));
var td = document.createElement("td");
td.appendChild(a);

That might not be 100%. It's been a while since I've coded in plain JS. JQuery makes it too easy.

edit: on that note...

var $a = $('<a>').append($('<span>').text(val.Name)).click(function() {
    showFlightsByName(val.Name); 
});
$('<td>').append($a).appendTo("WHEREVER");

Upvotes: 2

Matteo Tassinari
Matteo Tassinari

Reputation: 18584

Try this:

var str ="";
str += "<td><a onclick=\"showFlightsByName('"+val.Name+"')\" href=\"javascript:;\"><span>" + val.Name + "</span></a></td>";

Upvotes: 2

Shoe
Shoe

Reputation: 76240

"<td><a onclick='showFlightsByName("+val.Name+")' href='javascript:;'><span>" + val.Name + "</span></a></td>"

should be

"<td><a onclick=\"showFlightsByName('"+val.Name+"')\" href=\"javascript:;\"><span>" + val.Name + "</span></a></td>"

Upvotes: 4

Related Questions