Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I use querySelector on a parentElement which I know has the queried class

I can traverse to reach my target element like this:

this.parentElement.parentElement.style.backgroundColor = 'red';

But why does this not work even though I know for a fact that the second parentElement has the .container class:

 this.parentElement.parentElement.querySelector('.container').style.backgroundColor = 'red';

Shouldn’t .querySelector start the search from the current element?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example code:

document.querySelectorAll('button').forEach(btn => {

            btn.addEventListener('click', function(){

                //works:
                //this.parentElement.parentElement.style.backgroundColor = 'red';

                //error Cannot read properties of null:
                this.parentElement.parentElement.querySelector('.container').style.backgroundColor = 'red';

            })
            
        });
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>

        body{
            display: flex;
            flex-direction: row;
        }

        .container{
            width: 25vw;
            border-radius: 1rem;
            background-color: bisque;
            padding: 1rem;
            margin: 1rem;
        }

      
    </style>

</head>
<body>

    
    <div class="container">

        <h2>Element</h2>
        <div class="description">
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam magnam delectus a animi fugit, odio veritatis maxime nihil totam earum, voluptates facere dolores facilis exercitationem? Hic repudiandae optio totam doloribus.</p>
        <button>Color</button>
        </div>
    </div>

        <div class="container">

            <h2>Element</h2>
            <div class="description">
                <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam magnam delectus a animi fugit, odio veritatis maxime nihil totam earum, voluptates facere dolores facilis exercitationem? Hic repudiandae optio totam doloribus.</p>
            <button>Color</button>
            </div>
        </div>
            <div class="container">

                <h2>Element</h2>
                <div class="description">
                    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam magnam delectus a animi fugit, odio veritatis maxime nihil totam earum, voluptates facere dolores facilis exercitationem? Hic repudiandae optio totam doloribus.</p>
                <button>Color</button>
                </div>

            </div>
            
</body>
</html>

>Solution :

No. from docs:

The querySelector() method of the Element interface returns the first element that is a descendant of the element on which it is invoked that matches the specified group of selectors.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading