How to expand a two elements simultaneously with Javascript?

Asked

Viewed 119 times

0

I need to expand two elements. Both have the same class css. The name of class for expansion is .fechado a. One is in the element .accordion-seta and another in .accordion-sinal. It would be something like:

<div class="accordion-seta">
   <div class="fechado">
     <a href="#"><i class="fa fa-plus"></i></a>
   </div>
   <div class="accordion-sinal">
       <div class="fechado">
           <a href="#"><i class="fa fa-plus"></i></a>
       </div>
    </div>
</div>

How would the Javascript of this tagging look?

I know the code below expands the first.

document.querySelector(".accordion-seta .fechado a").click();

I need to open them both at once.

  • How are you opening these Divs? with CSS or JS? you can show the code?

  • I’m opening with JS

2 answers

0

Solved.

var i = window.setInterval(expandir, 1000);
function expandir(){
    var count = $(".fechado a").length;
    document.querySelector(".fechado a").click();
    var count = $(".fechado a").length;
    if(count == 0){
        clearInterval(i);
    }
}
  • What does this code do? I couldn’t understand it. I thought I wanted to increase the size of the div.

0


Exchange the querySelector for querySelectorAll. The querySelector returns only the first object found, while the querySelectorAll returns all matching objects. Then use the forEach, to iterate between objects. The forEach receives a function with one or two parameters: forEach(function(element) {}) or forEach(function(element, index) {}).

In this example I left below, I use only the element to accomplish the .click().

document
   .querySelectorAll('.accordion-seta .fechado a')
   .forEach(function(element) { 
      element.click(); 
   });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.