Created
January 20, 2012 13:59
-
-
Save Raynos/1647489 to your computer and use it in GitHub Desktop.
RadioNodeList polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
} | |
} | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For IE11 I had to change this to define the property on
HTMLCollection
instead ofNodeList
, sincemyform.elements.myradiosetname
returns anHTMLCollection
rather than aNodeList
.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 supportRadioNodeList
.