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
How to Calculate the Sum of Table Column in Angular 2+
Use Lodash to Find Objects in an Array Matches an Id (Complex, Drilldown)
How to Disable Eslint Rule Max Line Length for Paragraph in <Template> of Vue.Js
How to Make an Area Unclickable With CSS
How to Trigger Click Event on Select Element (Not Change)
How to Avoid the Setstate()/Render() Endless Loop When Passing Data from Child to Parent
Pass Node Js Variable to Ejs Template
Property Does Not Exist on Type 'Never' Typescript/React
How to Modify Chartjs Tooltip So I Can Add Customized Strings in Tooltips
Make Discord Bot Send Picture With Message With Nodejs
How to Only Remove the "Watch Later" and "Share" Buttons from Youtube Iframe Embed Player
How to Mute Video With JavaScript or Jquery
How to Look for Changes in Localstorage in Angular 4
How to Hash Passwords Before Posting and Then Using Bcrypt
Prevent Form Submission on Enter Key Press
How to Add Pm or Am Based on Hour from Input
Django Template JavaScript Passing a Python Variable to a JavaScript One