php - Can't get data attribute value -
am trying fetch data attribute dynamically created table using foreach
.currently getting value od data attribute serviceid of first row only.
this php code :
<?php foreach ($service_arr $service) {?> <tr class="odd gradex"> <td><button id="delete_btn" type="button" data-serviceid="<?=$service->service_id?>" class="btn btn-default" data-toggle="modal" data-target="#servicesdeletemodal">delete</button></td> </tr> <?php }?>
i want fetch value serviced table. if have 3 rows generated in foreach
loop, getting serviceid of first row (<tr>
).why that?
jquery code:
$('#delete_btn').click(function(){ var service_id = $(this).attr("data-serviceid"); console.log(service_id); });
please thanks
you have duplicate ids. id selector binding click first element in matched dom. should use .data()
set data attribute. , make sure click event binded when dom ready. this:
$(function(){$('.btn.btn-default').click(function(){ var service_id = $(this).data("serviceid"); console.log(service_id); });});
Comments
Post a Comment