Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  20] [ 5]  / answers: 1 / hits: 27200  / 8 Years ago, fri, september 2, 2016, 12:00:00

I need to get the value of userid, data-attribute from a html table and put this value into a var, but I wanna to this action without click action.



  <table id=tblList>
<tbody id=someTest>
<tr data-userid=801992084067>
<tr data-userid=451207954179>
<tr data-userid=310896831399>
<tr data-userid=863939754980>
<tr data-userid=1123542226482>
</tbody>
</table>


I have tried to do this like that, but the rowId is undefined.



 var rowId = $(#someTest tr).last().attr([data-userid]);

More From » jquery

 Answers
28

only Remove [] :



 var rowId = $(#someTest tr).last().attr(data-userid);


Final code :





<html>
<title>This is test</title>

<head>
</head>
<body>
<table id=tblList>
<tbody id=someTest>
<tr data-userid=801992084067>
<tr data-userid=451207954179>
<tr data-userid=310896831399>
<tr data-userid=863939754980>
<tr data-userid=1123542226482>
</tbody>
</table>

<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js></script>
<script>

$(document).ready(function(){

var rowId = $(#someTest tr).last().attr(data-userid);
alert(rowId);

})
</script>
</body>
</html>




[#60839] Tuesday, August 30, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;