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

311
Views
how to get last li element using javascript?

I am trying to get last li element of list . but it is not giving correct output. here is my markup

<div class="abc">
    <ul>
      <li>1</li>
      <ul><li>1.1</li><li>1.2</li></ul>
      <li>2</li>
       <ul><li>2.1</li><li>2.2</li></ul>
      <li>3</li>
        <ul><li>3.1</li><li>3.2</li></ul>
    </ul>
  </div>

trying like this

console.log(document.querySelector('.abc ul li:last-child').innerHTML)

output Element

<li>1.2</li>

Expected output

 <li>3</li>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

There are 2 problems

  • Your HTML Is invalid as ul is not a valid child of ul so the nested ones need to be actually inside a li

  • Use the > modifier to find direct descendents only in the selector

console.log(document.querySelector('.abc>ul>li:last-child').innerHTML)
<div class="abc">
  <ul>
    <li>
      1
      <ul>
        <li>1.1</li>
        <li>1.2</li>
      </ul>
    </li>

    <li>
      2
      <ul>
        <li>2.1</li>
        <li>2.2</li>
      </ul>
    </li>

    <li>
      3
      <ul>
        <li>3.1</li>
        <li>3.2</li>
      </ul>
    </li>

  </ul>
</div>

If you do just want the text (and not the nested ul) you could put that text in a span and select that only

console.log(document.querySelector('.abc>ul>li:last-child span').innerHTML)
<div class="abc">
  <ul>
    <li>
      <span>1</span>
      <ul>
        <li>1.1</li>
        <li>1.2</li>
      </ul>
    </li>

    <li>
      <span>2</span>
      <ul>
        <li>2.1</li>
        <li>2.2</li>
      </ul>
    </li>

    <li>
      <span>3</span>
      <ul>
        <li>3.1</li>
        <li>3.2</li>
      </ul>
    </li>

  </ul>
</div>

If you do just want the text (and not the nested ul) you could put that text in a span and select that only

about 4 years ago · Juan Pablo Isaza Report

0

You can just go for the javascript way like this.

li = document.querySelectorAll('.abc>ul>li');
console.log(li[li.length-1].innerText);
about 4 years ago · Juan Pablo Isaza Report

0

console.log(document.querySelector('.abc ul li:last-child').innerHTML)

This syntax of giving white space instead of adjacent selectors like ">" is used when we want to apply common styles to multiple class.

So replacing your whitespace with adjacent selectors like > will work in your case to select all its descendent childs.

console.log(document.querySelector('.abc>ul>li:last-child').innerHTML)
<div class="abc">
  <ul>
    <li>
      1
      <ul>
        <li>1.1</li>
        <li>1.2</li>
      </ul>
    </li>

    <li>
      2
      <ul>
        <li>2.1</li>
        <li>2.2</li>
      </ul>
    </li>

    <li>
      3
      <ul>
        <li>3.1</li>
        <li>3.2</li>
      </ul>
    </li>

  </ul>
</div>

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