Advertisement
ucielsola

botones dinamicos

Apr 11th, 2022
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div class="contenedor"></div>
  11. </body>
  12. <script defer>
  13. const contenedor = document.querySelector(".contenedor");
  14. const arrayBotones = ["uno", "dos", "tres", "cuatro"];
  15.  
  16. arrayBotones.forEach((btn) => {
  17. const boton = document.createElement("button");
  18. boton.innerText = btn; // btn se reemplaza por cada string que contiene el array
  19. boton.setAttribute("onclick", "reconocerBoton(event)");
  20. contenedor.appendChild(boton);
  21. });
  22.  
  23. function reconocerBoton(e) {
  24. const texto = e.target.innerText;
  25. switch (texto) {
  26. case "uno":
  27. alert("hola uno");
  28. break;
  29. case "dos":
  30. alert("hola dos");
  31. break;
  32. case "tres":
  33. alert("hola tres");
  34. break;
  35. case "cuatro":
  36. alert("hola cuatro");
  37. break;
  38. }
  39. }
  40. </script>
  41. </html>
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement