Send values in onclick

2

I'm trying to send two values in onclick of HTML , however, the second value becomes "undefined", how can I send these two values to the function? p>

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

No javascript:

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

When I run 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.

    
asked by anonymous 26.11.2017 / 05:03

1 answer

4

You need to put quotation marks 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 PHP \" around it will work:

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

and compile like this:

onclick='transfertoinput(this.id, "RPG")'
    
26.11.2017 / 10:55