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') );
});
});