Question

I know that this is probably very dummy question, but I have been struggling with quotation marks for half an hour now:

I have a JS function that fills some div with content and there is an url which triggers another JS function which looks like this:

content = content.concat("<a href='#' onclick=\'highlight(\'" + value + "\')\'>" + conversationid + "</a>");

I tried various escaping etc, but I cant seem to get it right... This is how my attempt (the above code )actually looks like when I check it in browser:

<a href="#" onclick="highlight(" something")">something</a>

Can you suggest correct form please? Thanks a lot!

Was it helpful?

Solution

Use ' in JS and " in HTML:

content = content.concat('<a href="#" onclick="highlight(\'' +
          value + '\')">' + conversationid + '</a>');

You don't need to escape " in a string when you use ' as delimitator

UPDATED: I did not realized that string is HTML and JS together, now it should work (added some escaped single quotes in the onclick attribute declaration)

OTHER TIPS

I realise i edited this rather alot but finally, i think it is the winner:

content = content.concat('<a href="#" onclick="highlight(\'' + value + '\')">' + conversationid + '</a>');

Edit as an alternative maybe you could pop the quotes as html?

content = content.concat('<a href="#" onclick="highlight(&#39;' + value + '&#39;)">' + conversationid + '</a>');

I think both will work :P

Here is a fiddle http://jsfiddle.net/LT6bC/

It looks like something is rewriting your single-quotes to double-quotes. If you can't stop it, you'll have to work around it. Assign the value string to a variable, and embed the name of the variable instead of trying to quote the string.

Well, in Javascript you can use singe qiotes to denote strings, so your function call in the HTML should look like this:

<a href="#" onclick="highlight(' something')">something</a>

HTML, on the other hand, requires you to use double quotes.

content = content.concat("<a href="#" onclick="highlight('" + conversationid + "')>" + conversationid + "</a>");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top