Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!