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

238
Visualizações
Loop through two arrays - check if they belong to a div - append html to div

Hopefully I explain this well, here goes...

So the below HTML displays a basic version of my HTML - essentially I need to append the H4 inside client content to the hover-item when hovered over (The hover item currently displays but is blank with no text inside).

So I use document.QuerySelectorAll to generate a Node list and then convert to an Array, loop over the client array.

Inside I use two for loops to loop over the client content array and hover item array and check if they are contained within the client div...

If they are, I then want to append the h4 to the hover item. Each h4 will be unique as they will be people's names so it needs to append the correct one.

Here's the example code & image of the result on the project itself: results image

const hoverItem = document.querySelectorAll(".hover-item")
const hoverItemArray = Array.from(hoverItem);

const clients = document.querySelectorAll(".client");
const clientsArray = Array.from(clients);

const clientContent = document.querySelectorAll(".client-content");
const clientContentArray = Array.from(clientContent);

clientsArray.forEach(item => {
  for (i = 0; i <= clientContentArray.length; i++) {
    for (e = 0; e <= hoverItemArray.length; e++) {
      if (item.contains(clientContentArray[i] && hoverItemArray[e])) {
        hoverItem[e].append(clientContentArray[i].innerHTML);
      }
    }
  }
})
<div class="results">
  <div class="client">
    <img class="client-image">
    <div class="client-content">
      <h4>Client Name</h4>
    </div>
    <div class="hover-item"></div>
  </div>

  <div class="results">
    <div class="client">
      <img class="client-image">
    </div>
    <div class="client-content">
      <h4>Client Name</h4>
    </div>
    <div class="hover-item"></div>
  </div>

  <div class="results">
    <div class="client">
      <img class="client-image">
    </div>
    <div class="client-content">
      <h4>Client Name</h4>
    </div>
    <div class="hover-item"></div>
  </div>

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

0

If I understand you correctly you basically want to move the h4 element from the .client-content to .hover-item when both elements are within a .client element. So in you example html it would be only for the first .client element.

You can do that with the code below. I added some commentary to the code to explain and added some css with colors to visualize what is happening.

//Loop over all client divs.
document.querySelectorAll('.client').forEach((clientDiv) => {
  //Check if the div contains both .client-content and .hover-item elements.
  if(clientDiv.querySelector('.client-content') && clientDiv.querySelector('.hover-item')) {
    //Append the h4 to to hover item.
    clientDiv.querySelector('.hover-item').append(clientDiv.querySelector('.client-content h4'));
  }
});
div {
  display: inline-block;
}

.hover-item {
  background-color: blue;
}

.client-content {
  background-color: red;
}

h4 {
  margin: 10px;
  background-color: yellow;
}
<div class="results">
  <div class="client">
    <img class="client-image">
    <div class="client-content">
      <h4>Client Name</h4>
    </div>
    <div class="hover-item"></div>
  </div>

  <div class="results">
    <div class="client">
      <img class="client-image">
    </div>
    <div class="client-content">
      <h4>Client Name</h4>
    </div>
    <div class="hover-item"></div>
  </div>

  <div class="results">
    <div class="client">
      <img class="client-image">
    </div>
    <div class="client-content">
      <h4>Client Name</h4>
    </div>
    <div class="hover-item"></div>
  </div>

about 4 years ago · Juan Pablo Isaza Relatório

0

First: You aren't listening for the "hover"-event (in JS mouseover) and need eventlisteners for that.


If you want to append the heading from the same div like the .hover-item you don't need arrays. You just need to select the previous element and its h4 then remove it and append it to the heading. Furthermore you can loop over the collection hoverItem directly without converting it to an array.

const hoverItem = document.querySelectorAll(".hover-item");

for (i = 0; i < hoverItem.length; i++) {
  hoverItem[i].addEventListener('mouseover', function() {
    const client_name = this.previousElementSibling.querySelector('h4');
    
    if (client_name) {
      client_name.remove();
      this.append(client_name);
    }
  });
}
<div class="results">
  <div class="client">
    <img class="client-image">
    <div class="client-content">
      <h4>Client Name 1</h4>
    </div>
    <div class="hover-item">Hover me!</div>
  </div>

  <div class="results">
    <div class="client">
      <img class="client-image">
    </div>
    <div class="client-content">
      <h4>Client Name 2</h4>
    </div>
    <div class="hover-item">Hover me!</div>
  </div>

  <div class="results">
    <div class="client">
      <img class="client-image">
    </div>
    <div class="client-content">
      <h4>Client Name 3</h4>
    </div>
    <div class="hover-item">Hover me!</div>
  </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