Smajl
Smajl

Reputation: 8005

Multiple quotation marks in one line

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!

Upvotes: 0

Views: 3067

Answers (5)

Pablo Lozano
Pablo Lozano

Reputation: 10342

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)

Upvotes: 2

Adween
Adween

Reputation: 2820

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/

Upvotes: 2

krish
krish

Reputation: 537

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

Upvotes: 0

aecolley
aecolley

Reputation: 2011

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.

Upvotes: 1

Kroltan
Kroltan

Reputation: 5156

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.

Upvotes: 0

Related Questions