1

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>

3 Answers 3

4
"<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>"
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

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

Comments

2

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.