Reputation: 1145
For example i have a for loop here which will generate some number lay inside a div
var countProfile = 0;
var tablehtml = "";
for (var i=0; i <10; i++){
tablehtml = tablehtml + '<div class="DataCount">'+countProfile+'</div>'
countProfile++;
};
$("#somewhere").html(tablehtml);
And I am trying to grab the last child for another method to use, I tried to use :last-child()
but it doesn't works in my application, am i doing it wrong?
var dataCounting = $(".DataCount:last-child").html();
console.log(dataCounting);
Upvotes: 0
Views: 80
Reputation: 115222
:last-child
selects last one in their parents which can be more than one if they are in different parent, instead try last()
or :last
$(".DataCount").last().html();
Or if there is only one parent and which contains different types of element then sometimes :last-child
will not work as you expected ( for more info : The Difference Between :nth-child and :nth-of-type ) so in that case use :last-of-type
instead.
$(".DataCount:last-of-type").html();
Upvotes: 3
Reputation: 18987
You have to use :last
selector...
$(".DataCount:last").html();
API documentation here https://api.jquery.com/last-selector/
Upvotes: 3