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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:39:30+00:00 2026-06-18T23:39:30+00:00

I need to figure out if I can perform the same javascript action on

  • 0

I need to figure out if I can perform the same javascript action on two identical elements with the same ID.

I have a box that appears twice on the page with the same content:

<div class="my_box" id="23"> Some bit 23 </div>

<div class="my_box" id="23"> Some bit 23 </div>

Then I find that element: (or those?)

var my_box_find = document.getElementsByClassName('my_box');

and after some processing/calculations want to replace the innerHTML of /both/ boxes:

document.getElementById(my_box_find[i].id).innerHTML = 'New bit 23';

This is an extremely simplified example. But it works when the element I am trying to change appears only once. If it appears twice, only the first instance will be changed…

Thinking out loud: I supposed I could append the ID with some random number to make each box pseudo-unique… but then this will do the calculations twice unnecessarily… I want them both to update at the same time with the same information.

  • 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-18T23:39:31+00:00Added an answer on June 18, 2026 at 11:39 pm

    IDs must be unique in a page. It is invalid to have non-unique element ids.

    If you want to operate on multiple elements, assign a class to each element and loop through the selected results. A class may be used multiple times, and an element may be assigned multiple classes.

    var els = document.getElementsByClassName('my_box');
    for (var i = 0; i < els.length; i++) els[i].innerHTML = 'New bit 23';
    

    Or, with jQuery:

    $('.my_box').html('New bit 23');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to catch that exception but I can't figure out which one it
So i need to figure out how i can get a record count value
Can't figure out how to use bbp_get_reply_author_role(), and i really need it, returns an
I can't seem to figure out what's wrong with my function.... I need to
I need to figure out a way to set ValidateRequest at model...I understand that
I understand that I need to figure out my own homework, but seeing that
I have a piece of code where I need to figure out if a
I have a need to perform a bitwise test within a JSP but can't
I have a large collection of objects and I need to figure out the
I am trying to figure out how I can perform authorization on my Rails

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.