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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:26:58+00:00 2026-06-13T17:26:58+00:00

Okay, obviously I’m new to javascript. I’m trying to use radio buttons to hide

  • 0

Okay, obviously I’m new to javascript. I’m trying to use radio buttons to hide divs by class, my function needs to hide all the divs with either class name “p12” or “p34” when a single radio is clicked.

BUT

This is only working for all the p12 divs…. what am I missing to make both?..

if (document.getElementById('numbofextras0').checked == true) {
    for(i=0; i<100; i++) 
        document.getElementsByClassName('p12')[i].style.display = 'none';

    for(i=0; i<100; i++) 
        document.getElementsByClassName('p34')[i].style.display = 'none';
}
  • 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-13T17:26:59+00:00Added an answer on June 13, 2026 at 5:26 pm

    I wouldn’t be surprised if it doesn’t like you possibly going outside the range of what the getElementsByClassName returns (but then again, I haven’t tested/checked what would happen). Try storing the results first, then looping through them.

    var p12s = document.getElementsByClassName('p12');
    for (var i = 0; i < p12s.length; i++) {
        p12s[i].style.display = "none";
    }
    
    var p34s = document.getElementsByClassName('p34');
    for (var i = 0; i < p34s.length; i++) {
        p34s[i].style.display = "none";
    }
    

    This way, it loops over the exact amount of elements (you don’t have hardcode 100 in) that each getElementsByClassName returns.

    The main problem is that when you hardcode 100 in, it will always loop from 0 to 99. If getElementsByClassName returns less than 100 elements, trying to access that index will return undefined and throw an exception when you try to do something like .style.display = "none";. Or what if there’s 160 elements returned? Only the first 100 will be modified. Making the call before the loop and storing the returned elements in a variable, then looping through that length, is the safe/correct way. The way you had it before, it was making a call to getElementsByClassName on every loop iteration – very inefficient – you only need to do it once!…and of course, hardcoding 100 wasn’t the best way to loop through the elements. It was all the right idea, just not the right order. I mean, technically, you could’ve done something like this:

    for (i=0; i<100; i++) {
        var el = document.getElementsByClassName('p12')[i];
        if (el) {
            el.style.display = 'none';
        }
    }
    

    But like I said, calling getElementsByClassName on every loop iteration isn’t very efficient.

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

Sidebar

Related Questions

I hope I can word this okay. I have multiple divs all called '.collapse-conten'
How can I re-use a function? Okay lets say I have this main function
Okay, so I'm obviously a newbie, but I need some help passing a variable
Okay, so i'm pretty new to C++ & the Windows API and i'm just
Okay. So I am currently trying to create a list of an interface in
Okay I thought this is going to be a piece of cake, but obviously
okay, I thought such code could not get wrong, but it obviously does: somewhere:
Okay, I've set up a bit of code which searching for all the pages
Okay, I'm currently trying to create bbcodes for my home made forum. It worked
Okay, as usual, I'm brand new to this... been researching and trial/erroring (yes that's

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.