Pass a String With Double and Single Quotes as Parameter to JavaScript Function

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



Leave a reply



Submit