1

I like to get the value of this attribute data-record-key , but I use this line it's return me undefined, Any help?

$("#testtable").find("tbody tr").attr("data-record-key")

<table id ="testtable">
<tbody>
<tr data-record-key="0"> 
<td>ALL</td></tr>
<tr data-record-key="1"> 
<td>ONE</td></tr>

</tbody>
</table>
MrCode
  • 63,975
  • 10
  • 90
  • 112
tsohtan
  • 830
  • 1
  • 13
  • 33
  • More info : [http://stackoverflow.com/questions/7261619/jquery-data-vs-attr][1] [1]: http://stackoverflow.com/questions/7261619/jquery-data-vs-attr – Thomas Aug 05 '13 at 09:45
  • 1
    Which jQuery version are you using? It should work fine: http://jsfiddle.net/S969L/1/ – Amin Abu-Taleb Aug 05 '13 at 09:46
  • Hi guys, i'm not sure who down votes your answer i think your's solution is work fine, mine actually work fine as well and finally i found it is because of the table contain a empty row which i'm not aware. So i up vote for all comments. Thanks for help. – tsohtan Aug 07 '13 at 03:35

5 Answers5

1

No need of using .attr instead use .data()

$("#testtable").find("tbody tr").each(function () {
    console.log($(this).data("record-key"));
});

JSFiddle

Updated with .each() to fetch for all td, helps to iterate the object.

Praveen
  • 55,303
  • 33
  • 133
  • 164
1

The .attr() method gets the attribute value for only the first element in the matched set. you should change your code.

$("#testtable").find("tbody tr").map(function(i){

return $(i).attr("data-record-key")
})
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
1

You could just directly select the elements with data-record-key, and get their values:

$(document).ready(function() {
    var recordKeys = $('[data-record-key]').map(function() {
        return $(this).data('recordKey');
    }).get();

    console.log(recordKeys); // outputs: [0, 1]
});

Here's a fiddle

billyonecan
  • 20,090
  • 8
  • 42
  • 64
1

Your code produces undefined because you aren't wrapping it in the DOM ready function. That means your code executes before the table element is available in the DOM. Change to:

$(document).ready(function(){
    $("#testtable").find("tbody tr").data("record-key");
});

.data() should be used instead of .attr() for this, but that won't cause your code to return undefined, it should still work (as long as you use the DOM ready).

Also, if you want to get all of the data-record-key's instead of just the first, you'll need a loop:

$(document).ready(function(){
    $.each($("#testtable").find("tbody tr"), function(i, val){
        console.log( $(this).data('record-key') );
    });
});
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • 1
    Can someone explain the downvote? This is the only answer that addresses the actual problem. – MrCode Aug 05 '13 at 09:53
1

You will get the value of the attribute by using:;

jQuery("#testtable tbody tr").attr("data-record-key")

But I'm not sure what you are trying to obtain. You will read the first value only, and a table is supposed to have many rows, right?

If what you want is to get the selected value, you can use:

jQuery("#testtable tbody tr.selected").attr("data-record-key")
opalenzuela
  • 3,139
  • 21
  • 41
  • -1 for a solution that works, and without giving any kind of explanation? Very nice... People, don't bother to answer or you will just lose popularity. – opalenzuela Aug 05 '13 at 09:58
  • @MrCode and others, I've given a +1 to all the answers that were downvoted, but still were correct answers after all, as a reward to the patience... – opalenzuela Aug 05 '13 at 10:42