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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:27:59+00:00 2026-06-09T18:27:59+00:00

i have a javascript to hide/unhide 2 elements in my html at the same

  • 0

i have a javascript to hide/unhide 2 elements in my html at the same time. It worked fine when it was only one but now for some reason some elements only dissapear when i click twice, i dont know where is the mistake, here’s the code in case anyone finds something wrong thanks.
The function unhides two elements whos div id’s are given (xdivID and divID) and keeps the parent and the grandparent visible (idParent idUncle)

function doubleunhide(xdivID, divID, idParent, idUncle) {
 var xitem = document.getElementById(xdivID);
 if(xitem){
 xitem.className='unhidden';
}
var item = document.getElementById(divID);
 if(item) {
   item.className='unhidden';
   }
   var elements = getElementsByClassName(document, "unhidden");
      var n = elements.length;
   for (var i = 0; i < n; i++) {
     var e = elements[i];
if ((e.id != divID) && (e.id != xdivID) && (e.id != idParent) && (e.id != idUncle)){
     e.className='hidden';
     }
  }}

And here’s how it is implemented in the html:

<div id="prj" class="hidden" style="margin-left:1em">
    <a href="javascript:doubleunhide('prj011', 'prj01numbers', 'prj', 'nil');" style="text-decoration:none; color: rgb(0,0,0)" id="prj01link">Project 01</a> <br>
    <a href="javascript:doubleunhide('prj021', 'prj02numbers', 'prj', 'nil');" style="text-decoration:none; color: rgb(0,0,0)" id="prj02link">Project 02</a> <br>
    <a href="javascript:doubleunhide('prj031', 'prj03numbers', 'prj', 'nil');" style="text-decoration:none; color: rgb(0,0,0)" id="prj03link">Project 03</a> <br>
    Project 04<br>
    Project 05<br>
    </div>
  • 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-09T18:28:00+00:00Added an answer on June 9, 2026 at 6:28 pm

    The problem is that then you use native node.getElementsByClassName() the resulting array is ‘live’ – it changes dynamically then you change page. In your case – then you execute e.className='hidden'; the elements array is changed and becomes smaller – current element are wiped out because it is no loger fit your search criteria what you passed in to getElementsByClassName(). But your code still thinks there is same elements and iterate over it. You trying get undefined node eventually, get errors and script broke.
    To avoid this you can iterate in reverse order:

       for (var i = n-1; i >=0 ; i--) {
         var e = elements[i];
         if ((e.id != divID) && (e.id != xdivID) && (e.id != idParent) && (e.id != idUncle)){
             e.className='hidden';
         }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some JavaScript that cycles through n div tags, displaying only one at
I have Javascript function defined in the main index.html file. One part of this
I have a JavaScript where I need to hide (and eventually show) some text
I have two Javascript scripts on a site. One is an accordion (show/hide) and
I have a website that is 1 html file and uses javascript to hide
I have got [hide] and [unhide] input buttons , the hide button shows some
I have a triggered javascript function to hide dds which have the same class
I have a simple hide/unhide div section using simple JavaScript. I need to have
Javascript I have code that will hide various sections in a MS CRM form
I have a toggle button, with a show/hide feature in javascript. How do 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.