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 7018651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:04:46+00:00 2026-05-27T23:04:46+00:00

so long story short, I’m testing something out, a simple rollover and i notice

  • 0

so long story short,

I’m testing something out, a simple rollover and i notice that for example, if i use this

css:
#box{ width:200px; height:200px; background-color:#F00;}

html:
<div id="box" class="boxClass">this is sample text</div>


Js:
var box = document.getElementById("box");

function changeBox(){
    //box.style.backgroundColor = "#1896fa";    
    box.style.backgroundColor = "#1896fa";

}

box.onmouseover = changeBox;

it works, simple as can be. simple mouseover, simple js…simple. Mouse rolls over and changes from red to bue…good, but then when i try to target the same div via

var box = document.getElementsByTagName("div");

or

getElementsByClassName("boxClass");

nothing happens. my ? is why?

reason im asking is because, i have a simple image gallery and wanted to implement gallery wide rollovers but didnt want to write them for EVERY image.

was trying to somehow group them all together via tagName(“img”) or ClassName(“boxClass”)

but to no avail. i know older browsers dont support some of these but i looked it up and saw all modern browsers sans Ie are a go. what am i missing?

any tips help direction is appreciated. thanks in advance.

  • 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-05-27T23:04:46+00:00Added an answer on May 27, 2026 at 11:04 pm

    Contrary to the getElementById function which returns a single element, the getElementsByTagName function returns a NodeList of DOM elements (which is normal as you could have multiple elements with this tag). So if you want to change the background color to all <div> elements in the DOM you will have to first loop through the elements of this collection.

    var boxes = document.getElementsByTagName("div");
    for (var i = 0; i < boxes.length; i++) {
        boxes[i].style.backgroundColor = "#1896fa"; 
    }
    

    Same remark stands true for the getElementsByClassName function.

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

Sidebar

Related Questions

Long story short: I've been layout out UI's with Html/CSS for about 8 years
Long story short I'm working with a legacy codebase that makes heavy use of
Long story short, we found files promoting prescription drugs on our server that we
Long story short, I'm developing a theme template for a blog that enables you
Long story short, the database I'm using needs to get looked at. Until that
Long story short, I'm making a custom Swing component that's basically a JTable with
Long story short, I have a long code that uses jQuery. Lots of files,
Long story short: var o="before"; x = function() //this needs to be an anonymous
Long story short, I have a struct (see below) that contains exactly one field:
Long story short: in a C# application that works with COM inproc-server (dll), I

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.