Passing a parameter to function with single quote
You can escape quotes/characters by prepending \
to it:
var string = 'my string with "double quotes" and \'single quotes\'';
var string = "my string with 'single quotes' and \"double quotes\"";
// ^ ^
Using a dynamic string:var foo = "bar with 'quotes'";
var string = 'my string with "double quotes" and ' + foo.replace(/'/g, "\\'");
//my string with "double quotes" and bar with \'quotes\'
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 function
Template literals inside function parameters doesn't work. You will need to simply provide parameters.
For example:
myFunc(`${myVar}`)
Can be simply used:myFunc(myVar)
Also, don't attach inline handler as suggested by CertainPerformance in the comment.Example including parameters being passed
<html>
<body> <script> // Parameters that get passed into function being fired by onclick var myParam = 'param';
function myParameterFunction() { return 'functionParam'; }
function myParameterFunctionWithParams(foo) { return foo + 1; } </script>
<button onclick="myFunction(myParam, myParameterFunction(), myParameterFunctionWithParams(2))"> Click me! </button>
<script> function myFunction(a, b, c) { console.log('fired', a, b, c) } </script></body>
</html>
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, "\\'");
call a function with single quotes
You should do either of this
replace outer single quotes with double quote
"Bewerbung(en) erfolgreich auf 'Sprachtest(s) bestanden' gesetzt!"
or escape the single quotes in middle
'Bewerbung(en) erfolgreich auf \'Sprachtest(s) bestanden\' gesetzt!'
if you do not do ths either way then its not considered as string
Are double and single quotes interchangeable in JavaScript?
The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.
Using the other type of quote as a literal:
alert('Say "Hello"');
alert("Say 'Hello'");
This can get complicated:alert("It's \"game\" time.");
alert('It\'s "game" time.');
Another option, new in ECMAScript 6, is template literals which use the backtick character:alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);
Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.
Related Topics
Play 2.X: How to Make an Ajax Request with a Common Button
Localstorage Object Is Undefined in Ie
How to Achieve Dynamic Scoping in JavaScript Without Resorting to Eval
Why Escape_JavaScript Before Rendering a Partial
Why Can't I Assign a New Value to "This" in a Prototype Function
Is There a Mechanism to Loop X Times in Es6 (Ecmascript 6) Without Mutable Variables
JavaScript .Replace Only Replaces First Match
Adding New Data to Firebase Users
Add Two Functions to Window.Onload
How to Parse a Date in Format "Yyyymmdd" in JavaScript
Detect If an Element Is Visible with Jquery
Render Object Properties in React
Backbone: Why Assign '$('#Footer')' to 'El'
JavaScript Replace() Method Dollar Signs
How to Chain These Functions Together with Promises
How to Generate Sourcemaps When Using Babel and Webpack