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

How to get last child from parent element using query selector method?

For example:

<div class="list-container">
  <p>hello<p>
  <p>hello<p>
  <p>hello<p>
  <p>hello<p>
</div>

How to get that last child element? I would like to point out that the children elements are dynamically created and keep increasing or decreasing according to the user’s whim. And I want to be able to pick the last child every time this happens. That’s why I ask. Plz, share me the syntax. Thank you.

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

>Solution :

.list-container > p:last-child will do

And please, avoid bloatware, such as jquery…

console.log(document.querySelector(".list-container > p:last-child"));
<div class="list-container">
  <p>hello1</p>
  <p>hello2</p>
  <p>hello3</p>
  <p>hello4</p>
</div>
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