Escape special characters when building html code using javascript -
hi build html code dynamically using javascript. problem gives me syntax error when try specify css class table using . think need escape special characters = , "" here. , tried give error of illegal syntax well.
for (var i=0; i<array.length; i++) { htmlstr = htmlstr+"<table class"+%3d+%22+"test"+%22+"><tr><th>a</th><td>"+array[i].a+"</td></tr><tr><th>b</th><td>"+array[i].b+"</td></tr><tr><th>c</th><td>"+array[i].c+"</td></tr></table>"; }
does know correct syntax this?
thanks in advance!
htmlstr=htmlstr+'<table class="test"><tr><th>a</th><td>' +array[i].a+'</td></tr><tr><th>b</th><td>' +array[i].b+'</td></tr><tr><th>c</th><td>' +array[i].c+'</td></tr></table>';
for example, if want encapsulate <input type="button" onclick="alert('hello, world!');"></input>
in javascript string, can't in 1 run, because code contains both double " , single ' quotes. should this:
var htmlcode='<input type="button" onclick="alert('+"'hello, world!'"+');"></input>';
you may use both single or double quotes define string variable in javascript:
var str0='hello, world!'; var str1="hello, world!";
they absolutely equal.
Comments
Post a Comment