JavaScript Removing class on click from open elements in an accordion remove class click anywhere,remove class click jquery,remove class

How do I keep only one definition visible at the same time in this example:

In other words:

Clicking on i button should toggle class on itself and the definition below the term, and remove active class from other buttons and open class from other definitions.

document.querySelectorAll("dl").forEach(dl =>
  dl.addEventListener("click", ({ target }) => {
    if (!target.matches("button")) return
    target.classList.toggle("active")
    target.parentElement.nextElementSibling.classList.toggle("open")
  })
)
dd {
  visibility: hidden;
  height: 0
}

.open {
  visibility: visible;
  height: auto
}

.active {
  color: DeepSkyBlue
}

abbr {
  pointer-events: none
}
<dl>
  <dt>aluminum
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>the chemical element of atomic number 13, a light silvery-grey metal.</dd>

  <dt>silver
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a precious shiny greyish-white metal, the chemical element of atomic number 47.</dd>

  <dt>gold
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a yellow precious metal, the chemical element of atomic number 79.</dd>

  <dt>platinum
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a precious silvery-white metal, the chemical element of atomic number 78.</dd>
</dl>
Answer:1

This could be a solution. You can loop through the elements which contain the class you want to toggle, if they do, then toggle it

document.querySelectorAll("dl").forEach(dl =>
  dl.addEventListener("click", ({ target }) => {

document.querySelectorAll(".open").forEach(function(element) {
    if(element.classList.contains("open")){  
        element.classList.toggle("open");
    }
});

document.querySelectorAll(".active").forEach(function(element) {
    if(element.classList.contains("active")){  
        element.classList.toggle("active");
    }
});

    if (!target.matches("button")) return
    target.classList.toggle("active")
    target.parentElement.nextElementSibling.classList.toggle("open")
  })
)
dd {
  visibility: hidden;
  height: 0
}

.open {
  visibility: visible;
  height: auto
}

.active {
  color: DeepSkyBlue
}

abbr {
  pointer-events: none
}
<body>
<dl>
  <dt>aluminum
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>the chemical element of atomic number 13, a light silvery-grey metal.</dd>

  <dt>silver
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a precious shiny greyish-white metal, the chemical element of atomic number 47.</dd>

  <dt>gold
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a yellow precious metal, the chemical element of atomic number 79.</dd>

  <dt>platinum
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a precious silvery-white metal, the chemical element of atomic number 78.</dd>
</dl>
</body>
Answer:2

It is the same thing but, you don't required to check condition everytime.

You can just use element.classList=""; to remove all give class from element.

No you don't have "active" and "open" class to any element, you need to use .classList.add("open") instead of .classList.toggle("open")

Check this:

document.querySelectorAll("dl").forEach(dl =>
  dl.addEventListener("click", ({ target }) => {

document.querySelectorAll("dd").forEach(function(element) {
    element.classList="";
});

document.querySelectorAll(".active").forEach(function(element) {
    element.classList="";
});

    if (!target.matches("button")) return
    target.classList.add("active")
    target.parentElement.nextElementSibling.classList.add("open")
  })
)
dd {
  visibility: hidden;
  height: 0
}

.open {
  visibility: visible;
  height: auto
}

.active {
  color: DeepSkyBlue
}

abbr {
  pointer-events: none
}
<dl>
  <dt>aluminum
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>the chemical element of atomic number 13, a light silvery-grey metal.</dd>

  <dt>silver
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a precious shiny greyish-white metal, the chemical element of atomic number 47.</dd>

  <dt>gold
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a yellow precious metal, the chemical element of atomic number 79.</dd>

  <dt>platinum
  <button type=button><abbr title="See Definition"><i>i</i></abbr></button></dt>
  <dd>a precious silvery-white metal, the chemical element of atomic number 78.</dd>
</dl>
Answer:3

Anchor tag not downloading file in IE, instead it gives the option to search for app in app store to open file. For chrome and FF this code is working fine. I don't know this is happening in windows 7 ...

Anchor tag not downloading file in IE, instead it gives the option to search for app in app store to open file. For chrome and FF this code is working fine. I don't know this is happening in windows 7 ...

  1. dynamically create anchor tag in javascript
  2. dynamically create anchor tag in c#
  3. dynamically create anchor tag jquery

it is concerning my final year project on web development. i want to change the contents of my page without reloading the page- i know this can be achieve using css /ajax or jquery but that would be ...

it is concerning my final year project on web development. i want to change the contents of my page without reloading the page- i know this can be achieve using css /ajax or jquery but that would be ...

  1. change detection method update content

I have created a somehow functional "deeply" nested array filtering functionality. This functionality displays car, which has a color "RED" assigned to them. However the cars, which "pass" the filter ...

I have created a somehow functional "deeply" nested array filtering functionality. This functionality displays car, which has a color "RED" assigned to them. However the cars, which "pass" the filter ...

  1. javascript deeply nested objects
  2. js deeply nested objects
  3. javascript flatten deeply nested array
  4. javascript check for deeply nested property

I am trying to pass an array of objects like "make":"Ford", "trim":"escape", "year":2014 into a JSON object car at following exaample var player = { "name":"John", "age":30, "car": [{"...

I am trying to pass an array of objects like "make":"Ford", "trim":"escape", "year":2014 into a JSON object car at following exaample var player = { "name":"John", "age":30, "car": [{"...

  1. pass array json
  2. pass array json ajax
  3. pass json array to spring mvc controller
  4. pass json array to mvc controller
  5. pass json array in postman
  6. pass json array to webmethod c#
  7. pass json array to web api c#
  8. pass json array to javascript function
  9. pass json array in ajax call
  10. pass json array to php
  11. pass json array to another activity android
  12. pass json array as parameter
  13. pass json array in url
  14. pass json array in intent
  15. pass json array from php to javascript
  16. pass json array to web api
  17. pass json array to mvc controller c#
  18. pass json array to mvc controller using ajax
  19. pass json array to controller
  20. pass json array to mvc controller java