• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1616
  • Last Modified:

Unable to escape quotes on javascript

Why is the following code giving me an error message?

<a href="#"  onclick="javascript:alert('This is a \"quotes\" test');">Test quotes</a>

Isn't it the escape characters take care of not taking the quotes " as the end of the string?

This is the error message I am getting:

unterminated string literal
[Break on this error] alert('This is a \
0
cabrera48
Asked:
cabrera48
  • 2
1 Solution
 
basicinstinctCommented:
do it like this:

<a href="#"  onclick='javascript:alert("This is a \"quotes\" test");'>Test quotes</a>

in your example the double quotes belong to the HTML, not the javascript...
0
 
basicinstinctCommented:
or you can do it like this:

<a href="#"  onclick="javascript:alert('This is a ' + String.fromCharCode(34) + 'quotes' + String.fromCharCode(34) + ' test');">Test quotes</a>
0
 
cabrera48Author Commented:
String.fromCharCode(34)  works, thanks !!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now