Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda