Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda