0

im passing a value retrieved from an ajax call to a function like this way:

nuevaFila+='<td><input type="button" value="Agregar" onclick="AgregarSuvenir('+item.CODSUVENIR+');"></td></tr>';

and we i clicked the button it throws an undefinided "variable" but is not a variable, i just wanted to pass it as a string, and the value passed to the function is taken as a variable, i tried

onclick="AgregarSuvenir("'+item.CODSUVENIR+'") and onclick="AgregarSuvenir('+item.CODSUVENIR+""')

but this fails

someone help me how to pass the value as string and not be taken a a variable.

thanks is advance

2 Answers2

0

This is a simple case of matching quotes. Because you use single quotes to delimit the string, you must add escaped quotes inside your function call in order for the inserted value to be treated like a string.

Here are two possible solutions that escape the quotations to allow for the parameter to the AgregarSuvenir function to be treated like a string:

nuevaFila+='<td><input type="button" value="Agregar" onclick="AgregarSuvenir(\''+item.CODSUVENIR+'\');"></td></tr>';

or:

nuevaFila+="<td><input type=\"button\" value=\"Agregar\" onclick=\"AgregarSuvenir('"+item.CODSUVENIR+"');\"></td></tr>";
Ben Arena
  • 148
  • 2
  • 11
0

found it!

it has to be formatted like stated on this post: pass string parameter in an onclick function

onclick="AgregarSuvenir(\''+item.CODSUVENIR+'\')
Community
  • 1
  • 1