Unable to escape quotes on javascript

Posted on 2007-10-16
Last Modified: 2013-11-19
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 \
Question by:cabrera48
    LVL 23

    Accepted Solution

    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...
    LVL 23

    Expert Comment

    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>

    Author Comment

    String.fromCharCode(34)  works, thanks !!!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    Read about why website design really matters in today's demanding market.
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now