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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -