Reputation: 217
i'm having a problem with clicking a button inside html table and get the value on the same row and pass it to an input FullName . how can i do that? thanks
<td><?php echo $r['signatoryname'] ?></td>
<td><?php echo $r['signatoryposition'] ?></td>
<td><?php echo $r['signatoryoffice'] ?></td>
<td>
<button type="button" class="btnedit btn btn-xs btn-success">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>Edit
</button>
</td>
<input id="Fullname" class="form-control" >
Upvotes: 1
Views: 2674
Reputation: 138
you can try this, or change your structure as per you hierarchy .
$('.btnedit').click(
function(){
var uName = $(this).parent().prev().html();
$(this).parent().next().children('.form-control').val(uName)
});
Upvotes: 1
Reputation: 25537
You can use like this,
$(".btnedit").click(function () {
var name = $(this).closest("tr").find("td:eq(0)").text();
$("#Fullname").val(name);
});
Closest("tr")
will return the clicked button's parent tr.
Then you can find the child td using its index.
find("td:eq(0)")
will return the first td
of selected tr
. In your case, it will return signatoryname.
Likewise, you can use find("td:eq(1)")
& find("td:eq(2)")
Upvotes: 2