Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda