Take action on parent elements via javaScript?

  • 0
    Hello everyone!!!! There is such HTML construction:
    <div class='block'>
        <div class='block__inner'>
              <button class='btn'></button>
        </div>    
    </div>


    When I click on the button with the btn class, I need to turn the actions with the div with the block class. How do I do this in javaScript? But there are many such blocks, it is the parent block that needs to be changed.
    JavaScript Adalyn Wallace, May 11, 2020

  • 2 Answers
  • 0
    .addEventListener("click", (e) => {
    console.log(e.target.parentNode)
    console.log(e.target.parentNode.parentNode)
    });
    Anonymous

  • 0
    Learn to google such basic questions.



    Your task can be broken down into 3 parts:

    1. Installing handler for clicking on the button

    2. Finding the parent of the element

    3. Performing the necessary actions with the parent



    For all three points, information is easily found through a search.
    Daniel Roy

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!