Skip to content

Instantly share code, notes, and snippets.

@mkoubik
Created April 14, 2015 14:58
Show Gist options
  • Save mkoubik/13932afaf7ed9aaf203c to your computer and use it in GitHub Desktop.
Save mkoubik/13932afaf7ed9aaf203c to your computer and use it in GitHub Desktop.
if (typeof HtmlFormControlsCollection == 'undefined') {
function HtmlFormControlsCollection( 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 );
}
HtmlFormControlsCollection.prototype = {
item: function ( i ) {
return this[i] != null ? this[i] : null;
},
namedItem: function ( name ) {
var ret = [];
for ( var i = 0; i < this.length; i += 1 ) {
if ( this[i].id === name || this[i].name === name ) {
ret.push(this[i]);
}
}
return ret.length ? ret : null;
}
};
}
var oldElements = HTMLFormElement.prototype.__lookupGetter__('elements');
HTMLFormElement.prototype.__defineGetter__('elements', function() {
var elems = oldElements.call(this);
if (typeof HtmlFormControlsCollection !== 'undefined' && elems instanceof HtmlFormControlsCollection) {
return elems;
}
return new HtmlFormControlsCollection(elems);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment