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

158
Views
How to get attribute inside a tag in Javascript for sorting function

i found this function which is working good but i need something a little more specific:

    function sortList() {
  var ul = document.getElementsByClassName("myUL")[0];
    
  Array.from(ul.getElementsByTagName("li"))
    .sort((a, b) => a.getAttribute('title').localeCompare(b.getAttribute('title')))
    .forEach(li => ul.appendChild(li));
}

So, i need to getAttribute title but from img tag which is inside my li instead of the li itself and i don't know ho to do this. Sorry for the stupid question but i'm a total noob just trying to create a simple archive for personal use.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use Element.closest()

   function sortList() {
      var ul = document.getElementsByClassName("myUL")[0];
      Array.from(ul.getElementsByTagName("li"))
        .sort((a, b) => a.querySelector("img").getAttribute('title').localeCompare(b.querySelector("img").getAttribute('title')))
        .forEach(li => ul.appendChild(li));
    }
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!