We help IT Professionals succeed at work.

How to use double-quotes string within single quotes?

__php__
__php__ asked
on
Why does this code fail? I want to have a double-quoted string within the alert!

Please note that I cannot change the: onclick="alert('....')" format!
<html>
<body>
<input type="button" value="alert" onclick="alert('This is\"s a test!')">
</body>
</html>

Open in new window

Comment
Watch Question

Author

Commented:
hmm.. it fails in the "code" section as well! I'll try posting here:

<html>
<body>
<input type="button" value="alert" onclick="alert('This is\"s a test!')">
</body>
</html>

Commented:
__php__

... depending on the function, you may need to add a semicolon at the end.....
For the \"s escape sequence...it it necessary to the final code?  because it seems that you've escaped the double quote properly...
<html>
<body>
<input type="button" value="alert" onclick="alert('This is\"s a test!');">
</body>
</html>

Open in new window

CERTIFIED EXPERT
Commented:
<input type="button" value="alert" onclick='alert("This is\"s a test!");'>
Commented:
This should work fine:

use &quot;

<html>
<body>
<input type="button" value="alert" onclick="alert('This is&quot;s a test!')">
</body>
</html>
Two problems...first one is a typo in line 3:

3. onclick=s"alert(

...should be:

3. onclick="alert


To fix the other problem, reverse the usage of quotes:

"alert('This is\" a test!')"

...becomes:

'alert("This is\" a test!")'

Full working version posted below in snippet.
<html>
<body>
<input type="button" value="alert" onclick='alert("This is\" a test!")'>
</body>
</html>

Open in new window

Forced accept.

Computer101
EE Admin

Explore More ContentExplore courses, solutions, and other research materials related to this topic.