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

187
Views
mezclar solo 5 de 20 elementos aleatorios de una lista html (ver el código)

buenos dias y gracias de antemano a toda la comunidad por este magnifico apoyo. Quien me puede ayudar a mejorarle una funcion a este codigo para devolver solo 5 elementos al azar de la lista completa de 20 elementos. Ingreso el código que ahora muestra aleatoriamente toda la lista. Gracias a toda la comunidad...

 var list = document.getElementById("items"), newlist = document.getElementById("shuffle"); function shuffle(items) { var cached = items.slice(0), temp, i = cached.length, rand; while(--i) { rand = Math.floor(i * Math.random()); temp = cached[rand]; cached[rand] = cached[i]; cached[i] = temp; } return cached; } function shuffleNodes() { var nodes = list.children, i = 0; nodes = Array.prototype.slice.call(nodes); nodes = shuffle(nodes); while(i < nodes.length) { list.appendChild(nodes[i]); ++i; } } window.onload = shuffleNodes;
 <form id="something"> <div class="classname"> <dl id="items"> <dd>1. item.<br></dd> <dd>2. item.<br></dd> <dd>3. item.<br></dd> <dd>4. item.<br></dd> <dd>5. item.<br></dd> <dd>6. item.<br></dd> <dd>7. item.<br></dd> <dd>8. item.<br></dd> <dd>9. item.<br></dd> <dd>10. item.<br></dd> <dd>11. item.<br></dd> <dd>12. item.<br></dd> <dd>13. item.<br></dd> <dd>14. item.<br></dd> <dd>15. item.<br></dd> <dd>16. item.<br></dd> <dd>17. item.<br></dd> <dd>18. item.<br></dd> <dd>19. item.<br></dd> <dd>20. item.<br></dd> </dl> </div> </form>

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

0

Si desea mantener solo 5 elementos barajados en la pantalla, debe modificar su shuffleNodes() borrando la lista y agregando 5 elementos en el ciclo while de la siguiente manera:

 function shuffleNodes() { var nodes = list.children, i = 0; nodes = Array.prototype.slice.call(nodes); nodes = shuffle(nodes); list.innerHTML = ""; while(i < 5) { list.appendChild(nodes[i]); ++i; } }
about 4 years ago · Juan Pablo Isaza Report

0

¡Buenos días, y gracias por el código! ¿podrías darme una última ayuda? Estoy intentando pero no puedo dividir esta lista (total 20 artículos); seleccionar elementos aleatorios; Mezcle: 2 elementos (entre 0.10) y 2 elementos (entre 10.20) y muestre todo aleatoriamente cuando cargue la página... en el snipped se divide la lista (entre 0 y 10) y se seleccionan los primeros 2 elementos....

 var list = document.getElementById("items"), newlist = document.getElementById("shuffle"); function shuffle(items) { var cached = items.slice(0,10), temp, i = cached.length, rand; while(--i) { rand = Math.floor(i * Math.random()); temp = cached[rand]; cached[rand] = cached[i]; cached[i] = temp; } return cached; } function shuffleNodes() { var nodes = list.children, i = 0; nodes = Array.prototype.slice.call(nodes); nodes = shuffle(nodes); list.innerHTML = ""; while(i < 2) { list.appendChild(nodes[i]); ++i; } } window.onload = shuffleNodes;
 <form id="something"> <div class="classname"> <dl id="items"> <dd>1. item.<br></dd> <dd>2. item.<br></dd> <dd>3. item.<br></dd> <dd>4. item.<br></dd> <dd>5. item.<br></dd> <dd>6. item.<br></dd> <dd>7. item.<br></dd> <dd>8. item.<br></dd> <dd>9. item.<br></dd> <dd>10. item.<br></dd> <dd>11. item.<br></dd> <dd>12. item.<br></dd> <dd>13. item.<br></dd> <dd>14. item.<br></dd> <dd>15. item.<br></dd> <dd>16. item.<br></dd> <dd>17. item.<br></dd> <dd>18. item.<br></dd> <dd>19. item.<br></dd> <dd>20. item.<br></dd> </dl> </div> </form>

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!