Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:48:08+00:00 2026-06-16T01:48:08+00:00

Something nice with plain javascript would be to be able to use forEach ,

  • 0

Something nice with plain javascript would be to be able to use forEach, map, filter, etc, on the items returned by document.querySelectorAll, document.getElementsBy* etc.

This’d lead to less dependency on jQuery, and simply cleaner code. Right now, this is how we can do it, in an ugly way:

[].forEach.call( document.querySelectorAll(sel), function(el) {
});

This is… verbose.

Any way to be able to use forEach and the likes right away on the elements returned?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:48:09+00:00Added an answer on June 16, 2026 at 1:48 am

    A naive way would be to do this if you tested on Chrome:

    NodeList.prototype.forEach = Array.prototype.forEach;
    

    This works. On Webkit. It doesn’t on Firefox though. Because FF returns an HTMLCollection…

    The most cross-browser way I’ve found:

    NodeList.prototype.forEach = HTMLCollection.prototype.forEach = Array.prototype.forEach;
    

    It doesn’t work on IE8 and lower though, because they choke when adding properties to host objects prototypes.

    Full list:

    NodeList.prototype.forEach = HTMLCollection.prototype.forEach = Array.prototype.forEach;
    NodeList.prototype.map = HTMLCollection.prototype.map = Array.prototype.map;
    NodeList.prototype.filter = HTMLCollection.prototype.filter = Array.prototype.filter;
    NodeList.prototype.reduce = HTMLCollection.prototype.reduce = Array.prototype.reduce;
    NodeList.prototype.reduceRight = HTMLCollection.prototype.reduceRight = Array.prototype.reduceRight;
    NodeList.prototype.every = HTMLCollection.prototype.every = Array.prototype.every;
    NodeList.prototype.some = HTMLCollection.prototype.some = Array.prototype.some;
    

    Or, to please our dear Bergi (and also because it is cleaner):

    ['forEach', 'map', 'filter', 'reduce', 'reduceRight', 'every', 'some'].forEach(
        function(p) {
        NodeList.prototype[p] = HTMLCollection.prototype[p] = Array.prototype[p];
    });
    

    Considering the link to perfectionkills, it’s mostly irrelevant there. The problem is that the DOM is mostly not behaving the same on browsers when extended. This modification is sane in all the browsers except IE <=8.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a nice day everyone, I have something to ask your hel, to better
So I rewrote my paths to something like: URL/really/nice/paths/ using mod_rewrite rules like this:
Perl offers this very nice feature: while ( <> ) { # do something
I want to use a javascript framework with MVC for a complex web application
I'm looking to branch out of plain mysql and mysqli and try to use
I'm having trouble getting C# and JavaScript/jQuery to play nice here. I have a
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
Something terrible as happened with my poor attempt at adding a CountIf forumula to
Something is wrong with my Rails app because when I run bundle install, I
something I've struggled with in the past and am struggling with today is preventing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.