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

210
Visualizações
How to sort images on click using vanilla JavaScript

I have a table of 8 images that I need to sort alphabetically when I click on any of the images. The image that has been clicked goes to the first position in the first row of the table. The next position in the first row will be the image whose name is alphabetically right after the name of the first one and so on for the other images. The images are stored in an array called 'flowers'.

Here is what I have so far:

      // Sort Images
  let imgClick = document.getElementsByTagName('img');
  for (var i = 0; i < imgClick.length; i++){
  imgClick.addEventListener("click", flowers.sort((a, b) => (a.name > b.name ? 1 : -1)))
};

The images don't get sorted when I click them.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This snippet will sort a list of already existing images in place. The images need to be in a common parent container (div#pics). By clicking on any of the images this one will be placed at the first position.

let imgs = [...document.querySelectorAll('#pics img')].sort((a,b)=>a.name.localeCompare(b.name));
    div=document.querySelector("div");
div.onclick=ev=>{
  if(ev.target.tagName!=="IMG") return;
  let i=imgs.indexOf(ev.target);
  imgs.slice(i).concat(imgs.slice(0,i)).forEach(img=>div.appendChild(img))
}
img {margin:8px}
<div id="pics">
<img src="pic1" name="c" alt="c"><img src="pic1" name="b" alt="b"><img src="pic1" name="a" alt="a"><img src="pic1" name="d" alt="d">
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

I hope this will give you an idea of how to implement that.

const container = document.getElementById("flowers");
const flowers = [
  "rose",
  "peruvian lily",
  "chrysanthemum",
  "orchid",
  "tulip",
  "carnation",
  "hyacinth"
 ].sort();
 
 
function PlaceFlowers(flowers) {
  container.innerHTML = "";
  
   for(let flower of flowers) {
    container.innerHTML += `<button id="${flower}" onClick='sortFlowers(event)'>${flower}</button>`;
 }
}

PlaceFlowers(flowers);
 
function sortFlowers(event) {
  const el = event.target.id;
  const indOfEl = flowers.indexOf(el);
  PlaceFlowers([...flowers.slice(indOfEl), ...flowers.slice(0, indOfEl)]);
}
<div id="flowers"></div>

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