Reputation: 4373
I am using an onclick javascript method with dynamic content as:
onclick="adviceEvent('${advice?.content}');"
advice content has somewhere in between string like:
Check these details carefully - you don't want to miss a offer because you h...
due to apostrophe in don't firbug says:
SyntaxError: missing ) after argument list
How can I resolve this.Any thoughts?
Upvotes: 0
Views: 74
Reputation: 4373
Thanks Guys for your Answers But, I resolved My issues By just swapping the position of single and double quotes:
onclick="adviceEvent('${advice?.content}');"
to
onclick='adviceEvent("${advice?.content}");'
mind the position of quotes.
and all perfect.
Upvotes: 0
Reputation: 2111
you should escape the '
on the server side
Do something like-
onclick="adviceEvent('${yourDynamicContent.replaceAll("'","\\'")}');"//your server side code to replace all '
replace all '
inside your dynamic content
on server side not on client side
As on the client side if your dynamic content
contains any of '
then on the client, it will be turns into some invalid javascript String.
Lets say your dynamic content prints hi'hello
,
then on client side your code will be
onclick="adviceEvent('hi'hello');"//'hi'hello' a invalid string in js
but if u escape '
from your dynamic content
the this will be like
onclick="adviceEvent('hi\'hello');"//'hi\'hello' a valid string in js
Upvotes: 0
Reputation: 19337
if its possible you need to create a code that will escape apostrophe before it will be executed by the browser so change the
onclick="adviceEvent('${advice?.content}');"
into
var str1 = '${advice?.content}'.replace("'","\\'");
onclick="adviceEvent('"+str1+"');"
I think using another extra variable will do just to get away with all the confusion between escaping quotes within the codes itself.
Upvotes: 1