I am building html at server side. The code sample is as below. My intention is to emit html like this
<a href="" onclick="doSomething('Test Value');">Test</a>
My Code Sample
string html="";
string param="Test Value";
html+="<a href='' onclick='"+"doSomething('"+param+"')'>Test</a>"
What am I doing wrong? The resultant html is bizzare
try this:
string html="";
string param="Test Value";
html+="<a href=\"#\" onclick=\""+"doSomething('"+param+"')\">Test</a>";
Your end result for that is <a href='' onclick='doSomething('Test Value')'>Test</a>
The onclick portion is the one giving you the problem. You cannot use ' to enclose the value if you are trying to call a js function (IE: doSomething in your case here)
Try this:
string html="";
string param="Test Value";
html += "<a href=\"\" onclick=\"" + "doSomething('" + param + "')\">Test</a>";
You want some thing like this.
html+=<a href="javascript:doSomething('"+param+"')">Test</a>;
and your function is:
function doSomething(param) {
alert("Inside anchor tag.");//For Test
}
Note:But it is browser dependent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.