Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  58] [ 4]  / answers: 1 / hits: 72386  / 11 Years ago, wed, october 16, 2013, 12:00:00

How do I pass a parameter to a javascript function with ' included



var name =Lauren O'Donald;

var htmlAnch='<a onclick=javascript:selectEmployee(1100,'+name+');return false;
href=javascript:void(0);>O'Donald, Lauren</a>';

$(document).append($(htmlAnch));


The javascript function is not executing since the name 'Lauren O'Donald' contains single quote.



How can I add a parameter with ' and prepare dynamic html to make it work?



Here is the dynamic code to generate



 var rows = new StringBuffer();

$(data).each(function(index) {
rows.append(String.format('<tr><td><a href=No.aspx
onclick=javascript:selectEmployee({3},{1} {2});return
false;>{0}</a></td></tr>',
String.format({0}, {1}, this.Surname, this.FirstName),
this.Surname,
this.FirstName,
this.Id
));
});

More From » jquery

 Answers
97

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'

[#74955] Tuesday, October 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chase

Total Points: 78
Total Questions: 106
Total Answers: 93

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
chase questions
Thu, Mar 31, 22, 00:00, 2 Years ago
Thu, Jul 1, 21, 00:00, 3 Years ago
Sat, Dec 12, 20, 00:00, 4 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
;