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

  • SEARCH
  • Home
  • 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 7963069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:31:21+00:00 2026-06-04T05:31:21+00:00

Do getElementsByClassName (and similar functions like getElementsByTagName and querySelectorAll ) work the same as

  • 0

Do getElementsByClassName (and similar functions like getElementsByTagName and querySelectorAll) work the same as getElementById or do they return an array of elements?

The reason I ask is because I am trying to change the style of all elements using getElementsByClassName. See below.

//doesn't work
document.getElementsByClassName('myElement').style.size = '100px';

//works
document.getElementById('myIdElement').style.size = '100px';
  • 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-04T05:31:22+00:00Added an answer on June 4, 2026 at 5:31 am

    Your getElementById code works since IDs have to be unique and thus the function always returns exactly one element (or null if none was found).

    However, the methods
    getElementsByClassName,
    getElementsByName,
    getElementsByTagName, and
    getElementsByTagNameNS
    return an iterable collection of elements.

    The method names provide the hint: getElement implies singular, whereas getElements implies plural.

    The method querySelector also returns a single element, and querySelectorAll returns an iterable collection.

    The iterable collection can either be a NodeList or an HTMLCollection.

    getElementsByName and querySelectorAll are both specified to return a NodeList; the other getElementsBy* methods are specified to return an HTMLCollection, but please note that some browser versions implement this differently.

    Both of these collection types don’t offer the same properties that Elements, Nodes, or similar types offer; that’s why reading style off of document.getElements…(…) fails.
    In other words: a NodeList or an HTMLCollection doesn’t have a style; only an Element has a style.


    These “array-like” collections are lists that contain zero or more elements, which you need to iterate over, in order to access them.
    While you can iterate over them similarly to an array, note that they are different from Arrays.

    In modern browsers, you can convert these iterables to a proper Array with Array.from; then you can use forEach and other Array methods, e.g. iteration methods:

    Array.from(document.getElementsByClassName("myElement"))
      .forEach((element) => element.style.size = "100px");
    

    In old browsers that don’t support Array.from or the iteration methods, you can still use Array.prototype.slice.call.
    Then you can iterate over it like you would with a real array:

    var elements = Array.prototype.slice
        .call(document.getElementsByClassName("myElement"));
    
    for(var i = 0; i < elements.length; ++i){
      elements[i].style.size = "100px";
    }
    

    You can also iterate over the NodeList or HTMLCollection itself, but be aware that in most circumstances, these collections are live (MDN docs, DOM spec), i.e. they are updated as the DOM changes.
    So if you insert or remove elements as you loop, make sure to not accidentally skip over some elements or create an infinite loop.
    MDN documentation should always note if a method returns a live collection or a static one.

    For example, a NodeList offers some iteration methods such as forEach in modern browsers:

    document.querySelectorAll(".myElement")
      .forEach((element) => element.style.size = "100px");
    

    A simple for loop can also be used:

    var elements = document.getElementsByClassName("myElement");
    
    for(var i = 0; i < elements.length; ++i){
      elements[i].style.size = "100px";
    }
    

    Aside: .childNodes yields a live NodeList and .children yields a live HTMLCollection, so these two getters also need to be handled carefully.


    There are some libraries like jQuery which make DOM querying a bit shorter and create a layer of abstraction over “one element” and “a collection of elements”:

    $(".myElement").css("size", "100px");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, getElementById() and getElementsByClassName() cannot locate many of the elements on gmail.
If i have something like this : function foo(class){ for(i=0; i<(class.length);i++){ return document.getElementsByClassName(class)[i]; }
Im trying to switch a getElementById to getElementsByClassName for a project like this: http://jsfiddle.net/2waZ2/21/
I found this function: document.getElementsByClassName = function(c){ for(var i=0,a=[],o;o=document.body.getElementsByTagName('*')[i++];){ if(RegExp('\\b'+c+'\\b','gi').test(o.className)){ a.push(o); } } return
Some DOM selection methods like getElementsByClassName() are common to HTMLDocument and HTMLElement. Is there
Am I missing something with getElementsByClassName() and querySelectorAll()? In Firefox 9 and Chrome 17
I get an array of DOM objects using getElementsByClassName : a = document.getElementsByClassName(foo); At
Why xhr.responseXML.getElementsByClassName('clazz') doesn't work? Here is the js: var xhr = new XMLHttpRequest(); xhr.onreadystatechange
I have a snippet of code like this: var profileLinks = new Array(); for
What is the best method to retrieve an array of elements that have a

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.