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

91
Visualizações
Append icon on both divider spans

I'm fairly new to JavaScript and are playing around with it to get a better understanding of it.

I have this problem where I want to append a to two span with the id of "divider" but somehow my forEach method only appends the icon on the last divider element.

Why is that 🤔?

Here is the HTML markup:

  <div class="container "> 
    <div class="row d-flex justify-content-center align-items-center min-vh-100"> 

    <div class="col left d-flex justify-content-center align-items-center">HI 👋</div>
      
    <div class="col right d-flex justify-content-center align-items-center flex-column">
         <span id="hours"></span>
         <span id="divider"></span>
         <span id="minutes"></span>
         <span id="divider"></span>
         <span id="seconds"></span>
    </div> 
    </div>
  </div>

Here is the JS:

const showHours = document.getElementById('hours'); 
const showMinutes = document.getElementById('minutes'); 
const showSeconds = document.getElementById('seconds'); 

 let dividers = document.querySelectorAll('#divider'); 
 let icon = document.createElement('i'); 
 icon.className = "fas fa-arrow-down"; 
 
setInterval(showTime, 1000); 

function showTime() {
  let time = new Date(); 
  let h = time.getHours(); 
  let m = time.getMinutes(); 
  let s = time.getSeconds(); 
  
  h = h < 10 ? "0" + h : h; 
  m = m < 10 ? "0" + m : m; 
  s = s < 10 ? "0" + s : s; 
  
showHours.innerHTML = h + ":"; 
showMinutes.innerHTML = m + ":"; 
showSeconds.innerHTML = s;   
}

dividers.forEach(divider => {
 divider.append(icon);
})

Link to project: https://codepen.io/andreasbruhn/project/editor/AYMmrP

Hope someone can help me understand, and sorry for this rookie question but i simply can't seem to wrap my head around this...

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

0

One thing that does comes to my mind immediatly - you are using an id for the divider. An can only used to identify an unique element and the same id cannot be given to multiple elements. What you want is to use a class, because the same class can be given to multiple elements.

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