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.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

3 Answers3

1

You need to change

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

into

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

Lennholm
  • 7,205
  • 1
  • 21
  • 30
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());" />
Falci
  • 1,823
  • 4
  • 29
  • 54
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.

BrandonFlynn-NB
  • 384
  • 2
  • 14
  • 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. – espresso_coffee May 11 '17 at 19:01