I am looking to be able to pass a javascript variable as data in an html table row element and have the data logged when that row element is click. The code that is appending/creating the table rows is inside an ajax call. Outside of the call in a click function I want to click on a table row that was created/appened and get the data variable. Here is the current code
$.ajax({
...
var name = 'superman'
$('#myTable tbody').append('<tr class = 'table_class' id = 'table_id' data-person = name></tr>');
});
...
$('#myTable tbody').on('click','table_class', function(){
console.log($(this).dataset.name);
}
The expected result of the console log is to be: superman.
2
Answers
You have more errors:
$(‘#myTable tbody’).on(‘click’,’.table_class’, function(){
The snippet:
If you are adding the class to the
<tr>
, make sure you include at least one<td>
child so that you can actually click on the row.