delete confirm dialog is only worked for the first row in a table

408 Views Asked by At

This is the example of my table, I got 2 icons there for edit action and delete. When delete, I want to make a popup confirmation dialog. It works only when I clicked the first row's delete icon.
For other rows, the jQuery event is missing (not working).

enter image description here

This is my .php coding file.

<tbody>    
//data connection with db
while ($row = mysql_fetch_array ($res))
{
    //data rows
?>
<tr class="odd" role="row" id="row_5"> 
    <td align="center">
        <a href="#"><img src="../images/sys_icon_edit.png" width="20" height="20" alt="Edit"></a> 
        <a id="id-btn-dialog2" href="#"><img src="../images/icon_trash.png" width="20" height="20" alt="Delete"></a>
    </td>   
    //other data fields here
</tr>    
<?php
}
?>     
</tbody>

This is the jquery related to it.

jQuery(function($) {

                $( "#id-btn-dialog2" ).on('click', function(e) {e.preventDefault();
                $( "#dialog-confirm" ).removeClass('hide').dialog({
                        resizable: false,
                        width: '320',
                        modal: true,
                        title_html: true,
                        buttons: [{
                                html: " Delete ",
                                "class" : "btn btn-danger btn-minier",
                                click: function() {
                                    $( this ).dialog( "close" );
                                }},
                                {
                                html: " Cancel ",
                                "class" : "btn btn-minier",
                                click: function() {
                                    $( this ).dialog( "close" );
                                }
                            }]
                    });
                });
})  

Please help me how can I do it? I'm just on beginner level to both php, jQuery and javascript.
That jQuery code is what I found on the internet, and I just changed the title text, not functional code.

Thanks in advance.

2

There are 2 best solutions below

2
On BEST ANSWER

Instead of id, use a class for your removal buttons, and set the handler to your body tag for dynamically added rows support.

$( "body" ).on('click', ".delete", function(e) {
            e.preventDefault();
            $( "#dialog-confirm" ).removeClass('hide').dialog({
                    resizable: false,
                    width: '320',
                    modal: true,
                    title_html: true,
                    buttons: [{
                            html: " Delete ",
                            "class" : "btn btn-danger btn-minier",
                            click: function() {
                                $( this ).dialog( "close" );
                            }},
                            {
                            html: " Cancel ",
                            "class" : "btn btn-minier",
                            click: function() {
                                $( this ).dialog( "close" );
                            }
                        }]
                });
            });
1
On

You're using ID / # for multiple instances of an element.

Try using a class instead.

For more information, and research visit this link.

This is an Example of how I would do it.