JS Polyfill
04 Apr 2022I was just trying to do a simple loop over a list of elements in JS
var inputs = document.getElementsByTagName('input');
inputs.forEach(function(input, index) {
// ...
});
but suprisingly to me I got an error:
VM52:1 Uncaught TypeError: inputs.forEach is not a function
at <anonymous>:1:8
inputs is an HTMLCollection, which is array-like object so it should be iterable— right?
Well it is— but in modern browsers, you need to use a for loop:
for (const i of inputs) {
// ...
}
Alternatively you can convert it to an Array with Array.from().
This SO answer made me curious what a “polyfill” was so I looked it up.
In web development, a polyfill is code that implements a feature on web browsers that do not support the feature. Most often, it refers to a JavaScript library that implements an HTML5 or CSS web standard, either an established standard (supported by some browsers) on older browsers, or a proposed standard (not supported by any browsers) on existing browsers.