Send value to javascript function

Asked

Viewed 36 times

0

Hello

I’m trying to send a value to function by clicking the button, which may be wrong ?

echo"<button id=".$nome." onclick='produtoComprado(/'".$detalhes."/')'>".$nome."</button>";

Thank you

  • 1

    $detalhes is what? Number, String, Object?

2 answers

1

Two simple quotes were missing when setting button id, test:

echo "<button id='".$nome."' onclick='produtoComprado(/'".$detalhes."/')'>".$nome."</button>";

1


When you use double quotes ", there is no need to concatenate this way, you can just put the variable, and if you need to put quotes inside the text, just use the backslash before \" to escape, the code gets cleaner and makes life easier.

echo "<button id=\"$nome\" onclick=\"produtoComprado('$detalhes')\">$nome</button>";

Follow the example running: https://repl.it/MxC/5

To learn more about the difference in quotation marks: Difference between single and double quotes in PHP

Browser other questions tagged

You are not signed in. Login or sign up in order to post.