Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created January 20, 2012 13:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Raynos/1647489 to your computer and use it in GitHub Desktop.
Save Raynos/1647489 to your computer and use it in GitHub Desktop.
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
Copy link

tremby commented Sep 13, 2016

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