Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameter as a string containg white spaces in javascript's onclick function

"<button onclick=editRecords('" + partId + "')>Add Quantity</button>";

If I pass the partId string without spaces, it's working fine. But if I pass 'MRF 01', it gives me

Uncaught SyntaxError: Unexpected token ILLEGAL.

Why is this and how can I fix that?

like image 886
Gurpinder Avatar asked Jun 21 '15 04:06

Gurpinder


1 Answers

Use &nbsp; whenever you pass space as a function parameter. If you are using js to generate onclick, use encodeURIComponent.

"<button onclick=editRecords('" + encodeURIComponent(partId) + "')>Add Quantity</button>";
like image 181
Unni Babu Avatar answered Sep 21 '22 08:09

Unni Babu