echoing document.getElementById() with php variable as id


Hi I have a list of buttons with different id’s like b_1, b_2, b_3 and so on. After that I have PHP code to interact with the database and get my results.
Then I have created a variable that receives the identity of the rows that it finds in the table. I would like to use the result to change the background color of the different buttons. It works fine if I write the literal identity "b_1" or "b_2" and so on but not in the form of a variable ($trans_boton).
I have tried in many ways and I can’t find the key. Thanks for lighting.

$trans_boton = 'b_'.$data['numero'];

if($facta == $respuesta){
echo "<script>"; echo "document.getElementById('b_1').style.backgroundColor='red';"; echo "</script>";}


You just need to put $trans_boton in quotes, ie:

echo "document.getElementById('$trans_boton')" ;

Answered By – tangledtrees

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published