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

121
Views
How can i select all elements using the help of this in ES6 class

I am trying to just access all child elements of a container. I created an ES6 class for this. I need class approach as per my project requirement. My target is, when i click on any child element, all the child elements needs to be selected. As an example now, i given a red border to all child elements when i click on any child element. If you see my code, following line is the main thing i need help let children = parent.querySelectorAll(".child"); now i used .child class here. But how can i use this.children here. The comple line should be like this let children = parent.querySelectorAll(this.children);. How can i achieve this. My code is as follows. If i get this, it will be very helpfull. Hopes anyone can be help me on it. Thanks in Advance!

class Selector{
constructor(parents, children) {

this.parents= document.querySelectorAll(parents);
this.children= document.querySelectorAll(children);

this.selectall=()=>{
this.children.forEach((elem)=>{
elem.addEventListener("click",(e)=>{
 let parent = e.target.parentElement;
 let children = parent.querySelectorAll(".child");
 children.forEach((elem)=>{elem.style.border="2px solid red"});
    })
  })
 }
}
}

let one = new Selector(".parent",".child");
one.selectall();
.parent {font-family:Arial, Helvetica, Sans-serif;}
<ul class="parent">
<li class="child">One</li>  
<li class="child">Two</li>  
<li class="child">Three</li>    
</ul>
    
    
<ul class="parent">
<li class="child">One</li>  
<li class="child">Two</li>  
<li class="child">Three</li>    
</ul>

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

0

Solution 1 (If parent has all children with child class):

Instead of

let children = parent.querySelectorAll(".child");

Try this

let children = Array.from(parent.children);

Solution 2 (If parent has children with class other than child)

Store the child class as well.

class Selector {
  constructor(parents, children) {
    this.parents = document.querySelectorAll(parents);
    this.childrenClass = children;
    this.children = document.querySelectorAll(children);

    this.selectall = () => {
      this.children.forEach((elem) => {
        elem.addEventListener("click", (e) => {
          let parent = e.target.parentElement;
          let children = parent.querySelectorAll(this.childrenClass);
          children.forEach((elem) => {
            elem.style.border = "2px solid red";
          });
        });
      });
    };
  }
}

let one = new Selector(".parent", ".child");
one.selectall();
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!