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

179
Views
No se pueden leer las propiedades de undefined (leyendo 'display')

Estoy tratando de alternar el texto que se muestra cuando hago clic en un botón para cada elemento que tengo en un carrusel.

Cuando uso "getElementByID" funciona bien, pero necesito usar "getElementsByClassName" porque es un campo repetidor en el backend y hay varios botones en todo el carrusel.

De todos modos, aquí está mi código:

 function toggleText(){ var x = document.getElementsByClassName("figure-caption-test"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } <button class="figure-button" id="figure-button" onclick="toggleText()"> REVEAL ANSWER </button> <figcaption class="figure-caption-test" id="reveal-text" style="display: none;"> Text that appears </figcaption>

Y el error que recibo es: no se pueden leer las propiedades de undefined (leyendo 'pantalla')

Cualquier ayuda es muy apreciada, gracias

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

0

getElementsByClassName devuelve una matriz de elementos. esta es mi solucion:

 function toggleText(){ var elms = document.getElementsByClassName("figure-caption-test"); Array.from(elms).forEach((x) => { if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } }) }
 <button class="figure-button" id="figure-button" onclick="toggleText()"> REVEAL ANSWER </button> <figcaption class="figure-caption-test" id="reveal-text" style="display: none;"> Text that appears </figcaption>

about 4 years ago · Juan Pablo Isaza Report

0

"getElementsByClassName" devuelve un objeto similar a una matriz, no un único objeto.

Puede iterar sobre el objeto usando un forEach:

 Array.prototype.forEach.call(x, function(element) { if (element.style.display === "none") { element.style.display = "block"; } else { element.style.display = "none"; } } );

Puede encontrar más sobre eso aquí: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

about 4 years ago · Juan Pablo Isaza Report

0

Creo que falta un bucle for y una variable de conteo.

Su código debería verse así:

 var x = document.getElementsByClassName("figure-caption-test"); var i; for (i=0; i < x.length; i++) { if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } }
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!