I'm trying to insert a div element on click of every anchor tag that is inside a set of elements of a dynamically generated html table. So far i'm only able to click the tag but not able to identify which row's tag is clicked.
//response from ajax call which is of json array
// form (data retrieved is actually a hashmap in the serverside)
$.each(response, function(key, value) {
$("#Table1Id").append('<tr><td><a id= '+curId+ 'class="achor" href='+""+ '>' + key + '</a></td><td>' + value.Date+ '<br>' + value.userName + '</td><td>' + value.userEmail + '</td><td>' + .... </tr>
});
//to capture the on click event and identify which row is clicked so
//that I can place the corresponding key's value in Div element.
$('#container1').on('click','a', function (event) {
event.preventDefault();
alert('Anchor clicked!');
var inkey = $('#container1 a').html() ;
alert("the key at the anchor clicked is " + inkey) ;
// this value is always the first key's value, in this case it's 1
//even if I click row 2's <a> tag.
var sibling = $(this).next('div');
if(sibling.is('.expanded')){
sibling.toggle();
}
else{
alert("reached else") ;
control reaches here but won't execute the below line
$(this).after("<div class='expanded'><table class='border1' width = 39% style= 'float: left'> " + "<td> date : <div align=left>" + inkey.value.cusip + "</div></td></tr></table></div>");
});
HTML table format after first ajax call :
Serial# Date Name Email
1 08/16 testuser blablabla
2 08/17 testuser blablabal
Please let me know to to handle this and if generating dynamic table this way is inefficient or poor programming plz provide suggestions to improve this code. Thanks.