The following code works fine just for the button in the very first row of the table. The buttons of the other automatically generated rows don't open any dialog. I guess the problem is that I am not assigning a different id
to each button. How can I do that? I read this page but nothing worked.
<table class="table-hovered">
<tr>
<th class="text-left big">TITOLO</th>
<th class="text-centered" align="center">
<img src="img/w.png" width="35" height="35" title="wikipedia" align="middle"><br>
wikipedia
</th>
</tr>
<? while($objResult = mysql_fetch_array($objQuery))
{ ?>
<tr>
<td class="text-left"><?=$objResult["titolo"];?></td>
<td class="text-centered">
<button id="trigger" class="btn">definizione</button>
<div id="dialog" style="display: none;" title="definizione">
<iframe frameborder="0" scrolling="yes" width="480" height="380" src="def.php?titolo=<?=$objResult['titolo'];?>"></iframe>
</div>
</td>
<script>
$("#trigger").click(function() {
$("#dialog").dialog("open");
});
$("#dialog").dialog({
autoOpen: false,
position: 'center' ,
title: 'definizione',
draggable: true,
width: 500,
height: 400,
resizable: true,
modal: true,
show: 'slide',
hide: 'fade'
});
</script>
</tr>
<? } ?>
</table>
Here is a possible solution:
Theoretically like this (adding
<?=$objResult["titolo"];?>
to the#trigger
IDs in the loop:what I am not sure about is the use of the PHP "$" inside the jQuery script, that might require a bit more tweaking.
The issue is because you are creating multiple elements with the same
id
attribute, where theid
must be unique within a singledocument
. Instead, use common classes on the#trigger
and from there find the related#dialog
to be shown. Try this:You can then assign a single event handler to the
.trigger
elements in either the<head>
or just before</body>
, like this: