Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat two nodelists

Tags:

javascript

I am trying to concat two nodelists using

var ul   = document.querySelector("ul"); var down = document.getElementsByClassName("mobile")[0]; var ul_child = Array.prototype.concat.call(ul.children,down.children); 

But this returns only two nodes from ul nodelist and ignore others. What is the most valid to concat two nodelsits? I would like to avoid brute looping them

like image 222
Darlyn Avatar asked May 31 '16 18:05

Darlyn


People also ask

How to merge two node lists?

(1) Create a new head pointer to an empty linked list. (2) Check the first value of both linked lists. (3) Whichever node from L1 or L2 is smaller, append it to the new list and move the pointer to the next node. (4) Continue this process until you reach the end of a linked list.

Can I use forEach on NodeList?

forEach() The forEach() method of the NodeList interface calls the callback given in parameter once for each value pair in the list, in insertion order.

Is a NodeList an array?

Note: Although NodeList is not an Array , it is possible to iterate over it with forEach() . It can also be converted to a real Array using Array.

Whats a NodeList?

A NodeList is a collection of document nodes (element nodes, attribute nodes, and text nodes). HTMLCollection items can be accessed by their name, id, or index number. NodeList items can only be accessed by their index number. An HTMLCollection is always a live collection.


1 Answers

Why don't you use one selector to select them at the same time than you do not need to concat them and you end up with an HTML Collection instead of an Array.

var elems = document.querySelectorAll("ul > li, .mobile > *");  console.log(elems);
<ul><li>x</li></ul>  <div class="mobile">y</div>
like image 150
epascarello Avatar answered Sep 23 '22 05:09

epascarello