Didi Willems Didi Willems - 1 month ago 4
Javascript Question

Jquery toggle with php variable doesn´t work

I have this toggle function to make a list item visible and invisible.
Without the variable id it works (only the first one opens of course). But with the variable it doesn't work at all. The variable is just a number, added to the list item id, which is well shown in the html code.
This is the function that I have:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
$("#button-" . $row['id'] . "").click(function() {
$("#bands-" . $row['id'] . "").toggle();
echo "<a id='button-" . $row['id'] . "'>bands</a>";

echo "<li id='bands-" . $row['id'] . "' class='clearfix list-group-item' style='display: none;'></li>";

( to echo each line is a bit ugly, I know, but it works :-P )


Your string escaping on JS side is wrong, the rest should work fine. On php side it was correct, because you are inside a string of echo.

$("#button-<?php echo $row['id']; ?>").click(function() {
    $("#bands-<?php echo $row['id']; ?>").toggle();