No. As specified in DOM4, it’s an HTMLCollection
(in modern browsers, at least. Older browsers returned a NodeList
).
In all modern browsers (pretty much anything other IE <= 8), you can call Array’s forEach
method, passing it the list of elements (be it HTMLCollection
or NodeList
) as the this
value:
var els = document.getElementsByClassName("myclass"); Array.prototype.forEach.call(els, function(el) { // Do stuff here console.log(el.tagName); }); // Or [].forEach.call(els, function (el) {...});
If you’re in the happy position of being able to use ES6 (i.e. you can safely ignore Internet Explorer or you’re using an ES5 transpiler), you can use Array.from
:
Array.from(els).forEach((el) => { // Do stuff here console.log(el.tagName); });
Related Posts:
- For-each over an array in JavaScript
- Short circuit Array.forEach like calling break
- Angular ForEach in Angular4/Typescript?
- Why is “forEach not a function” for this object?
- Short circuit Array.forEach like calling break
- Short circuit Array.forEach like calling break
- How do I convert a JavaScript forEach loop/function to CoffeeScript
- How to use forEach loop correctly in angular 2?
- Invariant Violation: Objects are not valid as a React child
- How to get all place details from a Place Details Request using Google Places API Web Service?
- How do I check whether a checkbox is checked in jQuery?
- SyntaxError: Unexpected token o in JSON at position 1
- What does the function then() mean in JavaScript?
- Heroku: “No default language could be detected for this app” error thrown for node app
- Loop inside React JSX
- Can’t resolve module (not found) in React.js
- Is there a difference between /\s/g and /\s+/g?
- How do I loop through or enumerate a JavaScript object?
- Getting Unexpected Token Export
- Setting a backgroundImage With React Inline Styles
- Filter array to have unique values
- How do I chop/slice/trim off last character in string using Javascript?
- What is causing this error – “Fatal error: Unable to find local grunt”
- Escape quotes in JavaScript
- Mouseover & Mouseout w/ Javascript
- Format JavaScript date as yyyy-mm-dd
- Programmatically change the src of an img tag
- Unable to preventDefault inside passive event listener
- JavaScript variable assignments from tuples
- jQuery $.cookie is not a function
- Setting background-image using jQuery CSS property
- Scroll Automatically to the Bottom of the Page
- npm WARN … requires a peer of … but none is installed. You must install peer dependencies yourself
- How do I check if string contains substring?
- What does “export default” do in JSX?
- Cannot find module ‘internal/fs’ after upgrading to node 7
- How can I select an element by name with jQuery?
- How can I group data with an Angular filter?
- jQuery disable/enable submit button
- Initializing an Array with a Single Value
- Is it possible to stop JavaScript execution?
- Remove commas from the string using JavaScript
- How can you remove all documents from a collection with Mongoose?
- TypeError: ‘undefined’ is not an object
- Uncaught TypeError: Cannot read property ‘appendChild’ of null
- javascript setTimeout() not working
- JQuery issue “TypeError: $.getJSON is not a function”
- Retrieve the position (X,Y) of an HTML element
- command not found: jest
- How to access Session variables and set them in javascript?
- Google Maps JS API v3 – Simple Multiple Marker Example
- HTML onchange (this.value)
- How to get the browser viewport dimensions?
- Getting HTML form values
- How to run a function when the page is loaded?
- RegEx: How can I match all numbers greater than 49?
- Multiple left-hand assignment with JavaScript
- Disabled href tag
- How to get child element by class name?
- TypeError getElementsByTagName is not a function issue
- Creating Accordion Table with Bootstrap
- Creating Multiple Modals on a Single Page
- How to check if the URL contains a given string?
- Check if url contains string with JQuery
- Javascript, viewing [object HTMLInputElement]
- Why onbeforeunload event is not firing
- etect the Enter key in a text input field
- Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (
) - What’s the equivalent of Java’s Thread.sleep() in JavaScript?
- wp_enqueue_script isn’t connecting my custom js file
- What do the args for Gutenberg subpackage “hooks” function “doAction” mean?
- How do I fire a snackbar notice in admin?
- Contact Form 7 – Give each checkbox a class?
- Should a WordPress plugin polyfill the global JS environment?
- is_page not working when loading javascript with add_action in functions.php
- Hide reply button after moveForm is called
- How to add custom javascript into from custom plugin
- remove inline scripts from the_content() produced by plugins?
- Help display image in single post using javascript zoom image..?
- How to set callback to execute after all the tinyMCE editors have been initialized?
- Correctly enqueue scripts of type=text/paperscript (PaperJs Library)
- useSelect second parameter
- Javascript in wordpress
- How do I make my wordpress post appear in a mouse hover preview?
- Do I just put the html in a page when enqueueng or do I also have to reference js file from the html page [closed]
- jQuery UI Tools: Scrollable: More than 1 instance possible?
- imporing HTML+javascript from REST API
- Uncaught TypeError: switchEditors.switchto is not a function
- how to add a custom javascript file to our theme
- Check if user can in javascript
- WordPress Guternberg Content For React Native
- Rest API and how to deal with it in server side
- Custom block update rendering when reused
- VisualComposer/WPBakery Page Editor: Is any JS event triggered after the Edition pop-in is shown?
- How do I add my own scripts to the add_action(‘wp_enqueue_scripts’) function?
- Post title and url not showing on google map marker [closed]
- Including js script at end of header
- Trigger a function during onload
- Embed Kajabi into WP
- Google AdSense Ad not showing up in WordPress Blog [closed]