6
"<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?

4
  • 1
    Shouldn’t this be "<button … onclick='editRecords(\'" + partId + "\')'> …"? Commented Jun 21, 2015 at 4:11
  • It gives Uncaught SyntaxError: Unexpected token } Commented Jun 21, 2015 at 4:15
  • 2
    Why? You could post some more of your code. I don’t know what else is interfering. Commented Jun 21, 2015 at 4:16
  • If i undo your code its working for string without spaces. Commented Jun 21, 2015 at 4:20

5 Answers 5

13

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>";
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot, Its working. But if i have to pass multiple paramaters then what should i do?
editRecords("mh&nbsp;01","secondParameter"); //same technique..use comma to seperate parameters, use &nbsp whenever you need a space..also remember that your editRecords function must exactly have 2 arguments.Example editRecords(i,j)
onclick=editRecords(' " + encodeURIComponent(partId) + " , " + encodeURIComponent(partName) +" ') Is above working fine for two params?
yes...it looks correct..you have encoded both parameters. Vote up if you are satisfied with my answer :) thanks Gurpinder ji :)
Should i decode the partId in editRecords function ?
|
6

From the poster himself. It's a primarily opinion based answer, there is nothing conventional in using single quotes (I have discovered C syntax and now I prefer double quotes...).

I would rather change the organisation of quotes in order to get a cleaner code. This will makes it more readable as well as more conventional (in my point of view):

var msg = 'Sometimes SO looks like a McDonalds drive through.';
var html = '<button onclick="clickHandler(\'' + msg + '\')">click me</button>';
function clickHandler (msg) { alert(msg); }
document.body.innerHTML = html;

Even more conventional would be to follow Felix Kling's wisdom (last line).

Comments

3

If an HTML attribute value is supposed to contain a space, then you have to use quotation marks to delimit the value.

Pay attention to the syntax highlight here:

<span foo=bar baz ></span>

This is an attribute foo with value "bar" and a boolean attribute baz.

On the other hand, this

<span foo="bar baz"></span>

is an attribute foo with value "bar baz". Notice how baz is highlighted differently?

To fix your issue, either put quotation marks around the value of onclick, or better, use a different way to bind the event handler.

Comments

0

I would suggest that this is the best option. You can set any attribute you want.

var e = document.createElement("span");
e.className = "close-li";
e.setAttribute('aria-hidden', 'true');
e.setAttribute('data-toggle', 'modal');
e.setAttribute('data-target', '#CreateModal');
e.setAttribute("onClick", "FunctionName(" + parameters + ");" );
e.innerText = "×";
$('#id').append(e);  //Id would be the place where you want to append. I used jquery so, I used append

Comments

0

String parameters with white space

<input type="button" onClick="return myFunction('10', 'your text hear')" />

or

PHP string variables with white space

<input type="button" onClick="return myFunction('<?php echo $id ?>', '<?php echo $designation ?>')" />

JS Function

function setVisibility(id, designation) {...your js code hear...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.