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

221
Views
¿Cómo cambio el color del texto con un botón en javascript?

Quiero cambiar una cierta parte del texto, llamada "cuadro" en html a un color diferente cuando se hace clic en un botón "btn" con javascript.

Pero aparece el error "Error de tipo no capturado: no se pueden leer las propiedades de undefined (leyendo 'estilo')".

Esto es lo que hice en js:

 const btn = document.getElementById('btn'); const box = document.getElementById('box'); btn.addEventListener('click', function onClick(event) { document.box.style.color = 'black'; });

Revisé diferentes sitios web y para otros casos, este código parece funcionar. ¿Alguien puede decirme dónde me equivoqué? ¡Gracias!

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

0

corrija la cuarta línea de su código para

 box.style.color = 'black';
about 4 years ago · Juan Pablo Isaza Report

0

 const btn = document.getElementById('btn'); const box = document.getElementById('box'); btn.addEventListener('click', function onClick(event) { box.style.color = 'blue'; });
 <button id = "btn">Click Me</button> <p><span id = "box">Some text.</span> Some more text.</p>

No es necesario poner document . Solo pones el element/variable.style.property = "value" El documento es un elemento del archivo: <html> y también puedes poner solo 1 elemento antes del estilo.

about 4 years ago · Juan Pablo Isaza Report

0

Tienes un pequeño error. lo he ajustado

  1. Ya obtienes el cuadro en una variable (cuadro)
  2. puede pasar al eventListener una función anónima

 const btn = document.getElementById('btn'); const box = document.getElementById('box'); btn.addEventListener('click', (event) => { box.style.color = 'red'; });
 <div id="box">123</div> <button id="btn">Change</button>

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!