javascript - how to get the value of the clicked table row? -
my question is: how value of clicked row , column?
the code have this:
js:
$.ajax({ type: 'post', url: url, data: json, success: function(data) { var response_array = json.parse(data); var columns = ['id', 'name', 'email', 'telephone', 'website', 'city']; var table_html = ' <tr>\n' + '<th id="id">id</th>\n' + '<th id="name">bedrijfnaam</th>\n' + '<th id="email">e-mail</th>\n' + '<th id="telephone">telefoon</th>\n' + '<th id="website">website</th>\n' + '<th id="city">plaats</th>\n' + '</tr>'; (var = 0; < response_array.length; i++) { //create html table row table_html += '<tr>'; (var j = 0; j < columns.length; j++) { //create html table cell, add class cells identify columns table_html += '<td class="' + columns[j] + '" >' + response_array[i][columns[j]] + '</td>' } table_html += '</tr>' }; $("#posts").append(table_html); }, error: function (jqxhr, textstatus, errorthrown) { alert('error: ' + errorthrown); } }); here html:
<div class="tabel"> <table id="posts"> </table> </div> i have tried following:
$('#posts').click(function(){ console.log("clicked"); var id = $("tr").find(".id").html(); console.log(id); }); sadly give me id of first row, no matter click.
any appreciated!
ramon
the below approach should able find id
$('#post').on('click', function(e){ var id = $(e.target).closest('tr').find(".id").html(); console.log(id) })
Comments
Post a Comment