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

84
Views
por qué btn.addEventListener no es una función. Estoy atascado en este problema y no pude encontrar ninguna solución. ¿Alguien puede ayudarme?

** Estoy aprendiendo la manipulación de Dom y es mi segundo día... ¿cómo puedo hacerlo como una función que generará un índice de color aleatorio que ya almacené en mi matriz? **

 const btn = document.getElementsByClassName("btn"); const color = document.getElementsByClassName("color") btn.addEventListener('click', function() { const randomNumber = getRandomNumber(); document.body.style.backgroundColor = colors[randomNumber] color.textContent = color[randomNumber] }) function getRandomNumber() { return Math.floor(Math.random() * colors.length); }
 <div class="btn">Click Me </div>

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

0

getElementsByClassName devuelve una matriz de elementos (cf doc ).

para obtener el primer elemento, debe obtener el primer elemento de la matriz document.getElementsByClassName("btn")[0]; o agregue una identificación al botón y consígala con la función document.getElementById('myButton')

 const btn = document.getElementsByClassName("btn")[0]; const color = document.getElementsByClassName("color") btn.addEventListener('click', function() { const randomNumber = getRandomNumber(); document.body.style.backgroundColor = colors[randomNumber] color.textContent = color[randomNumber] }) function getRandomNumber() { return Math.floor(Math.random() * colors.length); }
 <div class="btn">Click Me </div>

about 4 years ago · Juan Pablo Isaza Report

0

getElementsByClassName devolverá una matriz como objeto de elementos dom que coincidan con la clase. Debe seleccionar la entrada en esta matriz a la que desea apuntar:

 const btn = document.getElementsByClassName("btn")[0];

ver aquí para más detalles

about 4 years ago · Juan Pablo Isaza Report

0

getElementsByClassName devuelve una matriz, no un solo objeto DOM. Puede intentar agregar una identificación al elemento y usar getElementById

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!