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

130
Views
Problema de Javascript o más falta de conocimiento que otra cosa

Este es mi código actual a continuación. Está generando un número aleatorio para mí al cargar la página y funciona bien para el div de destino. Sin embargo, quiero que esto ejecute esta función dentro de 8 divs separados. Obviamente, no puedo tener 8 divs con la misma identificación, así que cambié esto a getElementByClassName y cambié la clase en mi div a vvv y me deshice de la identificación , pero por alguna razón, ¿esto no funciona?

Además, ¿necesito algo más en mi secuencia de comandos para llamar a esta función para más de un div? Cualquier ayuda muy apreciada!

 <script> let text = "VAC"; window.onload = function () { var output = document.getElementById("vvv"); setInterval(function () { output.innerHTML = Math.floor(Math.random() * (115 - 105 + 1)) + 105 + " " + text; }, 2000);//this value is the time between generating the number// }; </script>
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Está buscando getElementsByClassName (no getElementByClassName).

 let text = "VAC"; let outputs = document.getElementsByClassName("vvv"); let randomDivs = function() { for (let output in outputs) { outputs[output].innerHTML = Math.floor(Math.random() * (115 - 105 + 1)) + 105 + " " + text } } randomDivs() setInterval(randomDivs, 2000); //this value is the time between generating the number//
 <div class="vvv"></div> <div class="vvv"></div> <div class="vvv"></div> <div class="vvv"></div> <div class="vvv"></div> <div class="vvv"></div> <div class="vvv"></div> <div class="vvv"></div>

about 4 years ago · Santiago Trujillo Report

0

Puedes escribirlo así.

 <body> <div class="vvv">1234</div> <div class="vvv">1234</div> <div class="vvv">1234</div> <script> let text = "VAC"; var output = document.getElementsByClassName("vvv"); //you can use getElementsByTagName("div") as well setInterval(function (){ Array.from(output).forEach((item, index) => {item.innerHTML = Math.floor(Math.random() * (115 - 105 + 1)) + 105 + " " + text;} )}, 2000) </script> </body>

Nota: No me di cuenta de que ya se respondió con un enfoque similar. Puede probar getElementsByTagName("div") así como lo sugerido. Pero también tiene sus limitaciones.

about 4 years ago · Santiago Trujillo 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!