Languages
[Edit]
EN

JavaScript - create new NodeList() from node array

8 points
Created by:
Kevin
797

In this short article we would like to show how to create custom NodeList class in JavaScript.

By default NodeList class doesn't provide public constructor, so custom one lets to use new operator to create the object.

Quick solution:

 

Practical example

Edit

In this section you can find DOM-based solution.

 

Universal solution

Edit

This solution doesn't need DOM support, so it may be run under Node.js.

Hint: working under Node.js we should remove line with NodeList.prototype from the below source code and use other way to create elements than document object, e.g. some library or own custom logic.

 

See also

Edit
  1. JavaScript - create custom iterator for any Object

References

Edit
  1. NodeList - MDN Docs
  2. Proxy - MDN Docs

Alternative titles

  1. JavaScript - create new NodeList() from node array
  2. JavaScript - create custom NodeList class
  3. JavaScript - create own NodeList class
  4. JavaScript - convert array to NodeList
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join