Which quotes to use in string literal nested in another string

127 views Asked by At

I have in jQuery a function to change the html-content in a div, but the problem is that there is an onclick-function in it. I do not know which quote tags to use.

[q] = quote tag, that I do not know

$("#container div").html("<div id='overlay' onclick='goFullscreen([q]video1[q])'></div>");
3

There are 3 answers

7
Tushar On BEST ANSWER

Escape quotes:

Quotes in strings should be preceded by a backslash. This allows the JavaScript interpreter to distinguish a quote within the string from the quotes that serve as string delimiters.

$("#container div").html("<div id='overlay' onclick=\"goFullscreen('video1')\"></div>");
//                                                  ^^             ^     ^ ^^

DEMO

OR

$("#container div").html("<div id='overlay' onclick='goFullscreen(\"video1\")'></div>");
//                                                  ^             ^^      ^^

DEMO

Examples

string1='It\'s five o\'clock!';
string2="<A HREF=\"index.htm\">";

Alternatively, if your string includes single quotes only, then you can use double quotes as string delimiters, and vice versa. Here's an example:

string1="It's five o'clock!";
string2='<A HREF="index.htm">';

UPDATE

If video1 is variable:

$("#container div").html("<div id='overlay' onclick=\"goFullscreen('" + video1 + "')\"></div>");
//                                                  ^^             ^^          ^ ^^ ^^
0
Stand Still On

I assume this is what you wanted.

 $("#container div").html("<div id='overlay' onclick='goFullscreen(\"video1\")'></div>");
0
Varun On
$("#container div").html("<div id='overlay' onclick='goFullscreen(\"video1\")'></div>");

This way you can use any quote!