0

i am trying to toggle classes by using class name. My code is working fine when i am toggling using id. how can i make it work only one red should be visible at one time.

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

2
  • 1
    As an FYI: this is not the best way to create toggles nor lists. The element to click for the toggle should be focusable, which a div is not (by default). Something like a button or a are better options. Lists are better suited for ul or ol elements. Commented Oct 28, 2021 at 7:38
  • 1
    Spot the difference getElementsByClassName vs getElementById Commented Oct 28, 2021 at 7:41

4 Answers 4

4

As the name of getElementsByClassName suggest, it returns elementS, so you are getting HTMLCollection, not one element. Query the first one, like this:

function hideshowmenu() {
  var element = document.getElementsByClassName("box")[0];
  element.classList.toggle("bg-red");
}

or:

function hideshowmenu() {
  var elements = document.getElementsByClassName("box");
  elements[0].classList.toggle("bg-red");
}
Sign up to request clarification or add additional context in comments.

1 Comment

3

What is returned by document.getElementsByClassName("box"); is a collection. You need to specify the index to access the classList property.

You are doing:

var element = document.getElementsByClassName("box");
element.classList.toggle("bg-red");

When you should be doing

var element = document.getElementsByClassName("box");
element[0].classList.toggle("bg-red");

You could also loop through the elements if you want to toggle multiple values.

Example code fiddle https://jsfiddle.net/agy5jtb0/1/

Comments

2

getElementsByClassName("box") return a collection of elements, but you considered it as a single element that is your mistake. I suggest something like this:

html:

<div class="mainmenu " onclick="hideshowmenu(this)">RED1</div>
<div id="submenu" class="submenu">
    <div class="mainmenu" onclick="hideshowmenu(this)">RED2</div>
    <div id="box" class="box"></div>
</div>

js:

function hideshowmenu(el) {
    el.classList.toggle("bg-red");
}

Comments

2

Check this (not sure If is this what you need):

function hideshowmenu() {
  var elements = document.getElementsByClassName("box");
  for (let a of elements) {
    a.classList.toggle("bg-red");
  }
}
.bg-red{
  margin-top:10px;
  background-color:red;
  height:20px;
}
<div class="mainmenu " onclick="hideshowmenu(this)">RED1</div>
<div id="submenu" class="submenu">
    <div class="mainmenu" onclick="hideshowmenu(this)">RED2</div>
    <div id="box" class="box"></div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.