How to expand one two elements simultaneously with Javascript?

0

I need to expand two elements. Both have the same class css. The class name for expansion is .fechado a . One is in the element .accordion-seta and another in the .accordion-sinal element. 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 you like JavaScript to be enabled?

I know the code below expands the first one.

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

I need to open both at once.

    
asked by anonymous 15.09.2017 / 13:29

2 answers

0

Change querySelector to querySelectorAll . The querySelector returns only the first found object, while the querySelectorAll returns all matching objects. Then use 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 only use element to perform .click() .

document
   .querySelectorAll('.accordion-seta .fechado a')
   .forEach(function(element) { 
      element.click(); 
   });
    
15.09.2017 / 15:27
0

Resolved.

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);
    }
}
    
15.09.2017 / 14:15