javascript - jQuery not returning jquery.closest().text() -
i'm having issue jquery returning text closest td.index
. heres i'm working with:
$(document).ready(function() { $.getjson("http://ddragon.leagueoflegends.com/cdn/4.14.2/data/en_us/rune.json", function(response){ console.log(response.data); $.each(response.data, function (index, entry) { $('table.runes').append('<tr><td class="index">' +index+ '</td><td class="icon"> \ <div style="width:48px;height:48px;background:url(./assets/runes/rune0.png) -' +entry.image.x+ 'px -' +entry.image.y+ 'px no-repeat"> \ </div></td><td class="entry-name">' +entry.name+ '</td></tr> \ <tr><td></td><td class="entry-desc">' +entry.description+ '</td></tr>' ); }); }); jquery("body").on("click", "td.entry-name", function() { var gettext = $(this).closest('td.index'); alert(gettext.text()); }); });
as can see table generated using json/jquery, , there td
class name index
holds id number each row.
when clicking on td.entry-name
should closest td.index
, return text inside it, id number, alert. reason doesn't work.
i wondered if looking @ code if see issue.
because td.index
sibling of td.entry-name
not ancenstor use .siblings() instead of .closest()
jquery("body").on("click", "td.entry-name", function() { var gettext = $(this).siblings('td.index'); alert(gettext.text()); });
Comments
Post a Comment