Create a gist now

Instantly share code, notes, and snippets.

RadioNodeList polyfill
/*
demo :- http://jsfiddle.net/bjkkD/20/
Implements RadioNodeList :- http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#radionodelist
Implementation just adds `.value` as a property to `NodeList`.
Requires ES5 shim and ES5 shimmable browser. (Modern and IE8)
*/
;(function () {
Object.defineProperty(NodeList.prototype, "value", {
get: getRadioNodeListValue,
set: setRadioNodeListValue,
configurable: true
});
function getRadioNodeListValue() {
for (var i = 0, len = this.length; i < len; i++) {
var el = this[i];
if (el.checked) {
return el.value;
}
}
}
function setRadioNodeListValue(value) {
for (var i = 0, len = this.length; i < len; i++) {
var el = this[i];
if (el.checked) {
el.value = value;
return;
}
}
}
}());
@tremby

For IE11 I had to change this to define the property on HTMLCollection instead of NodeList, since myform.elements.myradiosetname returns an HTMLCollection rather than a NodeList.

Any comments on that? When would you have a NodeList instead?

I also wrapped it in a conditional: if (typeof RadioNodeList === "undefined") { so it doesn't run where browsers support RadioNodeList.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment