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

218
Views
Have div and span based on the number of elements

My intention is to create class="dot" based on the number of elements in the array called count, then use the following for loop:

for(var i = 0; i < count.length; i++)

So what I want to get is a single <span class="dot" onclick="currentSlide(1)"></span> where the currentSlide argument is automatically updated based on the for loop, therefore having i in place of 1,2,3 etc.

Is it possible to do this? Can you help me? I'm new to html and javascript.

<script>
var slideIndex = 1;
showSlides(slideIndex);

function currentSlide(n) {
  showSlides(slideIndex = n);
}

var count = ['img1','img2','img3','img4']
</script>
<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This should work by using template literal.

var slideIndex = 1;
//showSlides(slideIndex);

function currentSlide(n) {
  showSlides(slideIndex = n);
}

var count = ['img1', 'img2', 'img3', 'img4']
for (var i = 0; i < count.length; i++) {
  document.querySelector('div').innerHTML += `
 <span class="dot" onclick=currentSlide(${i})></span> 
 `
}
console.log(document.body.innerHTML)
<div style="text-align:center">
  <!--
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
   -->
</div>
OR:

var slideIndex = 1;
//showSlides(slideIndex);

function currentSlide(n) {
  showSlides(slideIndex = n);
}

var count = ['img1', 'img2', 'img3', 'img4']
for (var i = 0; i < count.length; i++) {
document.body.innerHTML += `
 <span class="dot" onclick=currentSlide(${i})></span> 
 `
}
console.log(document.querySelectorAll('span'))
<div style="text-align:center">

</div>

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!