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

67
Views
JS DOM changing parent element class

This is not homework or test task or whatever. Please give a solution or a hint if you can. I've got 3 divs with buttons, all the divs are absolutely the same, classes are the same, I can't add ID's attributes or anything else to these divs manually. All buttons are also the same with the same conditions, no IDs etc. So I need on a click to change the class for ONLY a parent element of a clicked button. How can I get to that particular parent if all divs and buttons are the same? Thanks

My HTML:

<body>
  <div class="off">
    <button>Button</button>
  </div>
  <div class="off">
    <button>Button</button>
  </div>
  <div class="off">
    <button>Button</button>
  </div>
</body>

JS:

document.addEventListener('click' , e => {

    let buttonClicked = e.target.matches('button');

    if ( buttonClicked ) {
        console.log("Button Clicked")
     }

})
about 4 years ago · Juan Pablo Isaza
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!