Srijith Ramachandran Srijith Ramachandran - 1 month ago 17
Javascript Question

Dom Navigation using Javascript

How can i access 3rd Element(H2) starting with the parent element "id="services-list".

Iam currently accessing using the below method.Is there a better way to do apart from the below. Please see i need purely Javascript.

var z = document.querySelectorAll('section[id="services-list"]');
z[0].firstElementChild.firstElementChild.firstElementChild.textContent


<section id="services-list" class="section container ">
<div class="row">
<div class="col-12 col-md-10 col-lg-6 offset-md-1">
<h2 class="section__title">Services</h2>
</div>
</div>
</section>


Regards,
Sree

Answer Source
let el = document.getElementById("services-list").getElementsByTagName("h2")[0];

console.log(el);

or with getElementsByClassName (but EI8- doesn't support getElementsByClassName)

let el = document.getElementById("services-list").getElementsByClassName("section__title")[0];

console.log(el);