Escaping Apostrophe (Single Quote) Character in JavaScript and HTML

How do I escape a single quote ( ' ) in JavaScript?

You should always consider what the browser will see by the end. In this case, it will see this:

<img src='something' onmouseover='change(' ex1')' />

In other words, the "onmouseover" attribute is just change(, and there's another "attribute" called ex1')' with no value.

The truth is, HTML does not use \ for an escape character. But it does recognise " and ' as escaped quote and apostrophe, respectively.

Armed with this knowledge, use this:

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change("ex1")' />";

... That being said, you could just use JavaScript quotes:

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";

Escaping single quotes in JavaScript string for JavaScript evaluation

The thing is that .replace() does not modify the string itself, so you should write something like:

strInputString = strInputString.replace(...

It also seems like you're not doing character escaping correctly. The following worked for me:

strInputString = strInputString.replace(/'/g, "\\'");

Escaping apostrophe (single quote) character in javascript and html

You need to have both ' and " in your string, so you will need a third way to delcare a string, you can use template strings for that. Declare your ba'r string as a template string and escape its apostrophe using a backslash \:

document.querySelector('#myDiv').innerHTML =    '<a href="javascript:foo(`ba\'r`)">link</a>';
function foo(data) { console.log(data);}
<div id="myDiv"></div>

How to escape the single quotes and double quotes at the same time

Use a backslash character (\) to escape it:

$("#divId").html('<div onclick="location.href=\"xxx\""');

Here are characters that need to be escaped in javascript strings:

  • Horizontal Tab: \t
  • Vertical Tab: \v
  • Nul char: \0
  • Backspace: \b
  • Form feed: \f
  • Newline: \n
  • Carriage return: \r
  • Single quote: \'
  • Double quote: \"
  • Backslash: \\

As stated in the comments, I would do something different in this case:

$('#divId').html('<div id="foo">');
$('#foo').click(function() {
window.location.href = 'xxx';
});

Escape single quote in JavaScript

Use jQuery to set the value;

function makeTitleEditable($titleContainer){
$titleContainer.replaceWith(
$("<input id='poll_title' name='poll[title]' type='text'>").val($titleContainer.text())
);
}

How can I escape a single quote?

You could use HTML entities:

  • ' for '
  • " for "
  • ...

For more, you can take a look at Character entity references in HTML.

Escape single quotes in title

You can use double quote:

myTitle = "abc '' def";
document.write('<img title="' + myTitle + '" src="http://www.example.com/image.png" />');

or escape the quote:

myTitle = "abc '' def";
document.write("<img title='" + myTitle.replace(/'/g, ''') + "' src='http://www.example.com/image.png' />");


Related Topics



Leave a reply



Submit