Send values on onclick

Asked

Viewed 1,793 times

2

I’m trying to send two figures on onclick of HTML, however, the second value is "Undefined", as I can send these two values to the function?

echo "<h3 id='$input' onclick='transfertoinput(this.id, $resp)'>$resp</h3><br>";

In javascript:

function transfertoinput(cl, valor) {
    document.getElementById(cl).value = valor;
}

When I call the function, this error appears:

Uncaught Referenceerror: RPG is not defined At Htmlheadingelement.onclick (indexjogo.php:1)

RPG would be the value of the variable '$Resp' and 'cl' is the id of the text input.

1 answer

4


You have to put quotes around $resp. What’s happening is that PHP compiles HTML like this:

onclick='transfertoinput(this.id, RPG)'

and then the this is interpreted as the element and RPG as a variable, not as a string. If you have it in PHP \" around it will work:

onclick='transfertoinput(this.id, \"$resp\")'

and compile so:

onclick='transfertoinput(this.id, "RPG")'

Browser other questions tagged

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