javascript - Get the last item from node list without using .length -
the following command
document.queryselectorall('#divconfirm table')[1].queryselectorall('tr')
gives node list 3 tablerow (tr) elements in it. if know list size, can access last element via.item(2)
.
is there way last element directly without resorting .length first?
there's @ least 1 way
var els = document.queryselectorall('#divconfirm table')[1].queryselectorall('tr'); var last = [].slice.call(els).pop();
but, following statement
but if not know length prior running script
makes no sense, have collection of elements, know length
var els = document.queryselectorall('#divconfirm table')[1].queryselectorall('tr'); var last = els[els.length - 1];
another option
document.queryselector('#divconfirm table:nth-child(2) tr:last-child');
Comments
Post a Comment