Search code examples
javascripthtmlquotes

JavaScript: Single quote ' causes error


I have a string with a ' inside it:

<a href="#" onclick="if (confirm('... &#039; ...')) { document.exampleFormName.submit(); }">example link text</a>

Unfortunately this does not seem to work. Firebug says "SyntaxError: missing ) after argument list" and you can see that the HTML entity has already been replaced by '.

What can I do to avoid this problem?


Solution

  • Try this

    <a href='#' onclick='if (confirm("... &#039; ...")) 
    { 
      document.exampleFormName.submit(); 
    }'> example link text
    </a>
    

    Because "#039" stands for single quote so ('...'...') in your previous code would fail because of this reason