Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take any element already referenced in JavaScript, give it an attribute we can find using a selector, find it as a list, remove the attribute, return the list.</p> <pre><code>function toNodeList(elm){ var list; elm.setAttribute('wrapNodeList',''); list = document.querySelectorAll('[wrapNodeList]'); elm.removeAttribute('wrapNodeList'); return list; } </code></pre> <p>Extended from bfavaretto's answer.</p> <hr> <pre><code>function toNodeList(elm, context){ var list, df; context = context // context provided || elm.parentNode; // element's parent if(!context &amp;&amp; elm.ownerDocument){ // is part of a document if(elm === elm.ownerDocument.documentElement || elm.ownerDocument.constructor.name === 'DocumentFragment'){ // is &lt;html&gt; or in a fragment context = elm.ownerDocument; } } if(!context){ // still no context? do David Thomas' method df = document.createDocumentFragment(); df.appendChild(elm); list = df.childNodes; // df.removeChild(elm); // NodeList is live, removeChild empties it return list; } // selector method elm.setAttribute('wrapNodeList',''); list = context.querySelectorAll('[wrapNodeList]'); elm.removeAttribute('wrapNodeList'); return list; } </code></pre> <hr> <p>There is another way to do this I thought of recently</p> <pre><code>var _NodeList = (function () { var fragment = document.createDocumentFragment(); fragment.appendChild(document.createComment('node shadows me')); function NodeList (node) { this[0] = node; }; NodeList.prototype = (function (proto) { function F() {} // Object.create shim F.prototype = proto; return new F(); }(fragment.childNodes)); NodeList.prototype.item = function item(i) { return this[+i || 0]; }; return NodeList; }()); </code></pre> <p>Now</p> <pre><code>var list = new _NodeList(document.body); // note **new** list.constructor === NodeList; // all these are true list instanceof NodeList; list.length === 1; list[0] === document.body; list.item(0) === document.body; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload