简体   繁体   中英

Getting the ID of a certain element in JQuery

How could I access the id of the TR/element where the user is in every time I click the .delete button.

foreach($devices as $device){ ?>
        <tr id="<?php echo $device['id']; ?>" class="edit_tr">
           <td>
            <input type="button" value="Delete" class="delete" id="delete_<?php echo $device['id']; ?>">
           </td>
</tr>

JQUERY:

$(".delete").click(function(){
        var ID=$(".edit_tr").attr('id');

with my code, ID only returns the first tr ID and not the ID where the user is currently focused at.

You should select the closest tr element and then call the .attr() method:

$(".delete").click(function() {
   var ID = $(this).closest(".edit_tr").attr('id');
$('.delete').click(function(){
  var id = $(this).attr("id").replace("delete_",""); 
});

Then, you can capture the same id that the tr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM