jquery - How to get atributes in <tr> tag when clicking an anchor inside a <td> that points to javascript function -
so need help.
i have query sql database wich returns array data table.
then have insert array data rows of table.
this code block fills table data array:
for ($i = 0; $i < $size; $i++) { print (" <tr> <td>".$users[$i][0]."</td> <td>".$users[$i][1]."</td> <td>*****</td> <td><a href='javascript:delete_user();'><img src='images/delete.png'/></a>/td> </tr> <tr>"); }
when click in anchor points javascript function delete_user();
the function delete_user();
function delete_user() { $.ajax({ type: "get", url: "delete_user.php" , data: { id: "table row id here" }, success : function() { window.location.href='utilizadores.php'; } }); }
i trying solve , if add atribute tag maybe jquery or js can it.
my first attempt:
for ($i = 0; $i < $size; $i++) { print (" <tr row-id='".$users[$i][0]."'> <td>".$users[$i][0]."</td> <td>".$users[$i][1]."</td> <td>*****</td> <td><a href='javascript:delete_user();'><img src='images/delete.png'/></a>/td> </tr> <tr>"); }
so, how can row-id value in parameter in ajax function on delete_user js function?
thanks in advance.
here's simplest way:
<td><a href='javascript:delete_user(\"{$users[$i][0]}\");'>
and
function delete_user( id ) {
alternatively:
<tr data-row-id='{$users[$i][0]}'> ... <td><a href='javascript:delete_user(this);'>
and
function delete_user( el ) { var id = $(el).closest("tr").data( "row-id" ); ...
Comments
Post a Comment