15

Hi I want to do the following, but don't know how to write the quotation marks

allSearchResults[0]="<li><a href="CXS101289/"> CXS101289/</a></li>";

It shall be quotation marks where the currently are.

2
  • 1
    Escape the inlying quotes: \" Commented Apr 14, 2011 at 10:52
  • 1
    Or use single quotes ' as 'outer' quotes. Commented Apr 14, 2011 at 10:54

5 Answers 5

20

Two ways times two

  1. mix single and double quotes:

    // single outside, double inside quotes
    allSearchResults[0] = '<li><a href="CXS101289/">CXS101289/</a></li>';
    

    or

    // double outside, single inside quotes
    allSearchResults[0] = "<li><a href='CXS101289/'>CXS101289/</a></li>";
    
  2. use one set of quotes but escape inside ones:

    // double escaped quotes
    allSearchResults[0] = "<li><a href=\"CXS101289/\">CXS101289/</a></li>";
    

    or

    // single escaped quotes
    allSearchResults[0] = '<li><a href=\'CXS101289/\'>CXS101289/</a></li>';
    

First approach with mixing is usually easier, because it presents less work since you only have to change the opening and closing quote.

Sign up to request clarification or add additional context in comments.

1 Comment

What if the content is dynamic? What I mean is, suppose "CXS101289" is coming from database as "CXS'101289"?
0

Just escape the quotes inside the a tag.

allSearchResults[0]="<li><a href=\"CXS101289/\"> CXS101289/</a></li>";

Comments

0

you can escape them like:

allSearchResults[0]="<li><a href=\"CXS101289/\"> CXS101289/</a></li>";

or use the other quotes :

allSearchResults[0]="<li><a href='CXS101289/'> CXS101289/</a></li>";

Comments

0
allSearchResults[0]="<li><a href='CXS101289/'> CXS101289/</a></li>";

or

allSearchResults[0]='<li><a href="CXS101289/"> CXS101289/</a></li>';

or

allSearchResults[0]="<li><a href=\"CXS101289/\"> CXS101289/</a></li>";

Comments

0

Another, newer and very nice method: Use "multiline strings"!

Writing it like this, with a backtick at the beginning and end of the string, you can use anything you like inside, and even make use of variable substitution:

let b = "myvar value";
let x = `
    <li class="myclass" onclick="myFunc('${b}')">
        ${b}
    </li>
`;

That case is hard to do otherwise, mixing quotation marks within the string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.