Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
JS: How to make sure only child div is toggled on click

Given example is working fine but red color is showing under Box2 only.

How to make sure if box1 is clicked then red should show below Box1, if Box2 is clicked box should show below box 2.

CODEPEN

function hideshowmenu() {
  var element = document.getElementsByClassName("box");
  element[0].classList.toggle("bg-red");
}
.bg-red {
  margin-top: 10px;
  background-color: red;
  height: 20px;
}
<div class="mainmenu " onclick="hideshowmenu()">BOX1</div>
<div id="box" class="box"></div>


<div class="mainmenu " onclick="hideshowmenu()">BOX2</div>
<div id="box" class="box"></div>

 

only one red should show at one time.

enter image description here

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

First remove the id's, you dont need them (and theyre not unique so they arent valid anyways).

Then youll need to iterate trough all your .mainmenu items and bin a click to hide all boxes and open the one right besides the item you clicked.

document.querySelectorAll(".mainmenu").forEach(function(menuElement) {
  menuElement.addEventListener("click", function() {
    document.querySelectorAll(".box").forEach(function(boxElement) {
      boxElement.classList.remove("bg-red");
    });

    this.nextElementSibling.classList.toggle("bg-red");
  });
});
.bg-red {
  margin-top: 10px;
  background-color: red;
  height: 20px;
}
<div class="mainmenu">BOX1</div>
<div class="box"></div>
<div class="mainmenu">BOX2</div>
<div class="box"></div>
<div class="mainmenu">BOX3</div>
<div class="box"></div>
<div class="mainmenu">BOX4</div>
<div class="box"></div>
<div class="mainmenu">BOX5</div>
<div class="box"></div>
<div class="mainmenu">BOX6</div>
<div class="box"></div>
<div class="mainmenu">BOX7</div>
<div class="box"></div>

about 4 years ago · Juan Pablo Isaza Report

0

You only trigger [0]

I would delegate

I also removed ID since IDs need to be unique

const container = document.getElementById("container");
const boxes = container.querySelectorAll(".box")
container.addEventListener("click", function(e) {
  const tgt = e.target;
  if (tgt.classList.contains("mainmenu")) {
    const thisBox = tgt.nextElementSibling;
    boxes.forEach(box => {
      if (box != thisBox) box.classList.remove("bg-red");
    })
    thisBox.classList.toggle("bg-red");
  }
})
.bg-red {
  margin-top: 10px;
  background-color: red;
  height: 20px;
}
<div id="container">
  <div class="mainmenu">BOX1</div>
  <div class="box"></div>
  <div class="mainmenu">BOX2</div>
  <div class="box"></div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You should use event object to get a target node, and then append div with the class after it. The after function will move the div each time.

<div  class="mainmenu" onClick="hideshowmenu(event)">BOX1</div>          
<div  class="mainmenu"  onClick="hideshowmenu(event)">BOX2</div>
const redBox = document.createElement('div');
redBox.classList.add('bg-red');

function hideshowmenu(event) {
  const elem = event.target;
  elem.after(redBox)
}

https://jsfiddle.net/hj0rgkfp/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!