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\'
Passing single and double quotes both in JavaScript function parameter from jstl (much tricky)
Encode it with "
.
function showPopup() { console.log(arguments); }
<a href="javascript:void(0);" target="_self" onclick="return showPopup('CRUISE ','ANOTHER " company"','123456','user@directbiller.com');"> Link</a>
How to pass a string that contains single and double quotes?
To do what you require you need to escape the single/double quotes within the name
string. To do that you could use a regex:
name = name.replace(/([\""|\''])/g, '\\$1');
$("#trID").attr('onclick', 'testFunction(' + name + ')');
However it should be noted that using inline event handlers, ie. the onclick
attribute in your example, is not good practice. You should use unobtrusive event handlers instead.
As you've already included jQuery in the page it would be achieved with an event handler and a data
attribute, like this:
let $div = $("#trID").on('click', function() {
console.log($(this).data('foo'));
});
$('button').on('click', function() {
$div.data('foo', (new Date()).getTime());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="trID" data-foo="14'6"">Click me to display value</div>
<button>Click me to change value</button>
How to pass multiple argument in js function with single quote
Maybe this should work?
var action_link = '<a class="dropdown-item" onclick="assign_id(\'' + aData['db value here'] + '\', \'' + 2 + '\')">Delivered</a>';
Or using template strings
onclick=`assign_id('${aData['id']}',2)`;
When should I use double or single quotes 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.
Pass variable as parameter to onclick function using double and single quotes in .append JQuery
Try changing to
onclick=\"editRow('" + item.id + "')\"/>
Related Topics
Pass Variable from Android to JavaScript Launched in Webview
Reactjs Dynamic Drop Down Populated With Map Function
Button Not Working on Mobile Devices But Works on Pc Bootstrap
How to Hide Blinking Cursor in Input Text
Detect Click into Iframe Using JavaScript
How to Get Checkbox Variables Using Ajax
How to Store Multiple Items in Local Storage
How to Close Current Tab in a Browser Window
Laravel Reactjs:Changes Are Not Reflecting After Change in My Js(Reactjs) File
Using Setstate to Change Multiple Values Within an Array of Objects - Reactjs
Change Image on Scroll Position
React Component Not Refreshing When Redux State Has Been Updated With Successful Action
Show Form Fields Based on Selected Option
Converting Excel Date Serial Number to Date Using JavaScript
How to Execute a JavaScript Function from the Java Part of Jsp
Recursively Find Keys on an Object