0

I have table row that I'm building in JavaScript. This row has an onClick function. I have to pass some values in this function but I'm getting an error. I think that my single and double quotes do not match. Here is my code:

insRow.innerHTML = "<tr><td><img src='../images/delete.png' border='0px' alt='Delete' title='Delete' onclick='pgDelete('"+fnObj.DATA+"','"+dType+"','"+tblID+"');' /></td></tr>";

Error message:

SyntaxError: expected expression, got '}'

If I inspect the element in dev tools this is what I get:

<img src="../images/delete.png" alt="Delete" title="Delete" onclick="pgDelete(" 8739','att','dba');'="" border="0px">

If anyone can help with this problem please let me know. Thank you.

4
  • What's the error? Commented May 11, 2017 at 18:47
  • Is this HTML injected with Javascript? If so, what does that code look like? Commented May 11, 2017 at 18:48
  • Well, look at it like a parser; you get to onclick=', so you're thinking, "they're using the single quote as a delimiter, so I'll stop at the next one". The next one is right after the opening parenthesis. The rest is garbage. Commented May 11, 2017 at 18:48
  • Yes this is injected with Javascript. Commented May 11, 2017 at 18:53

3 Answers 3

1

You need to change

onclick='pgDelete('"+fnObj.DATA+"','"+dType+"','"+tblID+"');'

into

onclick='pgDelete(\""+fnObj.DATA+"\",\""+dType+"\",\""+tblID+"\");'

Sign up to request clarification or add additional context in comments.

Comments

0

As a personal rule, I use "double quotes" for HTML and 'single quotes' for JS.

It means:

<img src="../images/delete.png" border="0px" alt="Delete" title="Delete" onclick="pgDelete(fnObj.DATA + ',' + dType + ',' + tblID);" />

However, there is a tip for concatenation using ',': use Array.join()

<img src="../images/delete.png" border="0px" alt="Delete" title="Delete" onclick="pgDelete([fnObj.DATA, dType, tblID].join());" />

Comments

0

I'm not certain what you're trying to concatenate but it seems like you have your parameters with unnecessary quotes and pluses. Does pgDelete() accept 3 parameters?

Try:
onclick='pgDelete(fnObj.DATA,dType,tblID);'

Instead of:
onclick='pgDelete('"+fnObj.DATA+"','"+dType+"','"+tblID+"');'

It would be helpful to see the origin of fnObj.DATA , dType and tblID; pgDelete() too.

1 Comment

All three arguments I will get once my process is successful. Ajax call send s parameters to the server. Then I get response in JSON format. I loop through object and populate values in each table row.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.