How to get the value of cell in a table when the user click on the next cell?
<table id="registrarsTable">
<c:forEach items="${requestScope.AllUsers}" var="user" varStatus="loop">
<tr class="userRow">
<td class="numberWidth">${loop.index + 1}</td>
<td class="nameWidth user" id="${user.userno}">${user.fullName}</td>
<td>
<button name="DeactivateBtn" id="deactivate" class="deactivate btn">deactivate</button>
</td>
</tr>
</c:forEach>
in jquery I want to get the value of attribute “id” from second td when user click on deactivate button.
I make like this
var userNo = $('td.user', $(this).parents(".userRow")).attr('id');
and like this
var userNo = $($(this).parent(".userRow") > "td").attr("id");
and no one works!
From the button (which is
this), traverse up to the parent (the<td>containing the deactivate button). Then, to the previous sibling<td>with the classuser, and get theidattribute.Alternatively, if you want to avoid relying on the fact the the deactivate button is in a
<td>after the user name’s<td>, you can do this:Which finds the parent
<tr>withclosestand then the<td class="user">withfind.Working example: http://jsfiddle.net/FishBasketGordo/Bk6ML/
API References:
parent()– http://api.jquery.com/parentprevAll()– http://api.jquery.com/prevAllclosest()– http://api.jquery.com/closestfind()– http://api.jquery.com/find