Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery variable within string, quotes within quotes

Tags:

jquery

I have the following jQuery code:

$(".container").append("<a href='javascript:void(0)'onClick='showField('"+data.name+"','"+data.text+"');'>Edit</a>");

Which is outputting (notice the quote problem before the 4 and before the >edit:

<a href="javascript:void(0)" onclick="showField("4','school_type');'>Edit</a>

When it should be outputting:

<a href="javascript:void(0)" onclick="showField('4','school_type');">Edit</a>
like image 413
Jonah Katz Avatar asked Jul 23 '12 14:07

Jonah Katz


2 Answers

try \" instead of ' in onClick:

$(".container").append("<a href='javascript:void(0)' onClick=\"showField('"+data.name+"','"+data.text+"');\">Edit</a>");
like image 123
mgraph Avatar answered Nov 19 '22 16:11

mgraph


You need to escape your "s with \

$(".container").append("<a href='javascript:void(0)' onClick='showField(\""+data.name+"\",\""+data.text+"\");'>Edit</a>");

Live DEMO

like image 5
Josh Mein Avatar answered Nov 19 '22 15:11

Josh Mein