Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a HTMLCollection

I'm trying to shim Element.prototype.children which should return a HTMLCollection

There is a window.HTMLCollection

However

var h = new HTMLCollection();
//TypeErrror: HTMLCollection is not a constructor

and

var h = Object.create(HTMLCollection.prototype);
h[0] = div;
h.item(0); 
// Could not convert JavaScript argument

Test Firefox 7 and Chrome

Apart from shimming HTMLCollection is there any way to interact with it?

Also provide feedback on this github issue if you can suggest a solution

like image 861
Raynos Avatar asked Oct 13 '11 12:10

Raynos


People also ask

What is an HTMLCollection?

An HTMLCollection is a list of nodes. An individual node may be accessed by either ordinal index or the node's name or id attributes. Note: Collections in the HTML DOM are assumed to be live meaning that they are automatically updated when the underlying document is changed.

How do you make an HTMLCollection array?

One way to convert an HTMLCollection to a JavaScript array is to use the slice method. We get divs by call querySelector to select all the divs in the HTML. We just call slice with divs and it'll return the div element objects in an array. We call slice on an empty array and pass in the divs HTML.

How do I find the HTMLCollection ID?

var t=document. getElementsByName('test'); alert(t);

Is an HTMLCollection an array?

An HTMLCollection is not an Array! An HTMLCollection may look like an array, but it is not. You can loop through an HTMLCollection and refer to its elements with an index. But you cannot use Array methods like push(), pop(), or join() on an HTMLCollection.


2 Answers

I think this is the proper way of creating HTMLCollection, which is handled by the browser.

var docFragment = document.createDocumentFragment();
docFragment.appendChild(node1);
docFragment.appendChild(node2);
var myHTMLCollection = docFragment.children;

Refs.:

https://stackoverflow.com/a/35969890/10018427

https://developer.mozilla.org/en-US/docs/Web/API/NodeList

https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection

https://www.w3schools.com/js/js_htmldom_nodelist.asp

like image 139
Alex Szücs Avatar answered Oct 23 '22 21:10

Alex Szücs


Here's how I would do it:

function MyHTMLCollection( arr ) {
    for ( var i = 0; i < arr.length; i += 1 ) {
        this[i] = arr[i];
    }

    // length is readonly
    Object.defineProperty( this, 'length', {
        get: function () {
            return arr.length;
        }
    });

    // a HTMLCollection is immutable
    Object.freeze( this );
}

MyHTMLCollection.prototype = {
    item: function ( i ) {
        return this[i] != null ? this[i] : null;
    },
    namedItem: function ( name ) {
        for ( var i = 0; i < this.length; i += 1 ) {
            if ( this[i].id === name || this[i].name === name ) {
                return this[i];
            }
        }
        return null;
    }
};

where arr is a regular array that contains all the DOM elements which should be inside the HTMLCollection.

To do list:

  • the argument arr should be checked beforehand: Is it an array? Are all elements of that array DOM elements?
like image 39
Šime Vidas Avatar answered Oct 23 '22 20:10

Šime Vidas