I have a problem with some dynamic buttons, these buttons are generated by a while loop:
while($sub_row=mysqli_fetch_array($piano_query)){
$piano=$sub_row['piano'];
$prezzo_piano=$sub_row['prezzo'];
$id_appartamenti=$sub_row['id_appartamenti'];
echo '<button type="button" class="simple-text piano" id="'.$id_appartamenti.'">piano '.$piano.'<br></button>';
So now, I want that when I click on those buttons (the id is set from a variable) something changes in a div. So I used a JavaScript, and I've tried to insert a php echo for the ID but it doesn't work:
$(document).ready(function() {
$("#<?php echo $id_appartamenti?> ").click(function() {
document.getElementById("prezzo").innerHTML="<?php echo $prezzo_piano;?> €";
});
})
and then something as to happen in the div with id=prezzo:
<div class="paragraph" id="prezzo">something has to happen here €</div>
dataattributes andclasshandlers, instead of making a js click handler for everyidelement dynamically.$prezzo_pianovariable, but you're adding the event listener on the ID from$id_appartamentiand in it, you replace the text in the button to the value of$prezzo_piano? That seems... odd. However, I would recommend you to do it as @IncredibleHat suggests. No need for separate event handlers for each button.