The problem is that even if 2nd button is clicked, it is finding first credentials only. So I want that if I click on the second button, it should give me that button's closest td with username and password
$(document).ready(function() { $(".jsLoginButton").each(function() { $(this).click(function() { //Get login details configuration.loginPage.LOGIN_ID = $('.loginID').closest('td').filter(':first').text(); configuration.loginPage.LOGIN_PASSWORD = $('.loginPassword').closest('td').filter(':first').text(); }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th style="padding: 3px">Username</th> <th style="padding: 3px">Password</th> <th style="padding: 3px">Action</th> </tr> </thead> <tr> <td style="padding: 3px" class="loginID">abc.user1@gmail.com</td> <td style="padding: 3px" class="loginPassword">Asdf1234!</td> <td style="padding: 3px"> <button class="jsLoginButton">login</button> </td> </tr> <tr> <td style="padding: 3px" class="loginID">xyz@gmail.com</td> <td style="padding: 3px" class="loginPassword">Asdf1234!</td> <td style="padding: 3px"> <button class="jsLoginButton">login</button> </td> </tr> </table>
The issue is because you're using a class selector to find all the .loginID
and .loginPassword
elements.
Instead you can fix the logic by using the this
keyword to reference the element that raised the event. From there you can use closest('tr')
to get the row, and find()
to select the required elements. Also note that you don't need the each()
call. You can apply click()
directly to the collection of all .jsLoginButton
elements. Try this:
var configuration = { loginPage: {} } $(document).ready(function() { $(".jsLoginButton").click(function() { var $tr = $(this).closest('tr'); configuration.loginPage.LOGIN_ID = $tr.find('.loginID').text(); configuration.loginPage.LOGIN_PASSWORD = $tr.find('.loginPassword').text() console.log(configuration); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th style="padding: 3px">Username</th> <th style="padding: 3px">Password</th> <th style="padding: 3px">Action</th> </tr> </thead> <tr> <td style="padding: 3px" class="loginID">abc.user1@gmail.com</td> <td style="padding: 3px" class="loginPassword">Asdf1234!</td> <td style="padding: 3px"> <button class="jsLoginButton">login</button> </td> </tr> <tr> <td style="padding: 3px" class="loginID">xyz@gmail.com</td> <td style="padding: 3px" class="loginPassword">Asdf1234!</td> <td style="padding: 3px"> <button class="jsLoginButton">login</button> </td> </tr> </table>
Use closest
to get the tr
, from there use find
to get the td
you want. Like this:
$(document).ready(function() {
$(".jsLoginButton").each(function() {
$(this).click(function() {
//Get login details
configuration.loginPage.LOGIN_ID = $(this).closest('tr').find('.loginID').text();
configuration.loginPage.LOGIN_PASSWORD = $(this).closest('tr').find('.loginPassword').text();
});
});
});
Try This
$(document).ready(function() { $(".jsLoginButton").click(function() { configuration.loginPage.LOGIN_ID = $(this).closest('td').siblings('.loginID').text(); configuration.loginPage.LOGIN_PASSWORD =$(this).closest('td').siblings('.loginPassword').text(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table> <thead> <tr> <th style="padding:3px">Username</th> <th style="padding:3px">Password</th> <th style="padding:3px">Action</th> </tr> </thead> <tr> <td style="padding:3px" class="loginID">abc.user1@gmail.com</td> <td style="padding:3px" class="loginPassword">Asdf1234!</td> <td style="padding:3px"> <button class="jsLoginButton">login</button> </td> </tr> <tr> <td style="padding:3px" class="loginID">xyz@gmail.com</td> <td style="padding:3px" class="loginPassword">Asdf1234!</td> <td style="padding:3px"> <button class="jsLoginButton">login</button> </td> </tr> </table>
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.