I am facing problems with escape ' character inside the onclick of an a tag
I have tried surrounding the string with double and single quotes. But I still face the issue
for(i in result.MenuItems){
var itemName = result.MenuItems[i].name;
html+= "<a onclick=return \'itemDescr(\""+itemName+"\",\""+restName+"\")\'>"+
'
<div>
' +
'
<div>
'+
'
<div style="float:left;width:50%;">
<h3 style="color:orange">'+itemName+'</h3>
</div>
'+
'
</div>
'+
'
</div>
'+
'</a>';
}
html+='</div>';
Problem area in the above code: html+= ""+ ' say if eiter name or rest name have ' then the code breaks during actual invocation of that function
The final html looks like::
::html:: <div id="items"><h1>Items</h1><a onclick=return
'itemDescr("Pho","Panda Garden")'><div><div><div
style="float:left;width:50%;"><h3
style="color:orange">Pho</h3></div></div></div></a><a onclick=return
'itemDescr("Chinese Dumplings","Panda Garden")'><div><div><div
style="float:left;width:50%;"><h3 style="color:orange">Chinese
Dumplings</h3></div></div></div></a><a onclick=return
'itemDescr("Gyoza","Panda Garden")'><div><div><div
style="float:left;width:50%;"><h3
style="color:orange">Gyoza</h3></div></div></div></a><a onclick=return
'itemDescr("Stinky Tofu","Panda Garden")'><div><div><div
style="float:left;width:50%;"><h3 style="color:orange">Stinky
Tofu</h3></div></div></div></a><a onclick=return 'itemDescr("Veggie
Burger","Panda Garden")'><div><div><div
style="float:left;width:50%;"><h3 style="color:orange">Veggie
Burger</h3></div></div></div></a></div>
When I click on one of the a tags I get below error:
Uncaught SyntaxError: Invalid or unexpected toke
itemDescr("Lamb Curry","Andala
It should have been :
itemDescr("Lamb Curry","Andala's");