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

190
Views
La expansión del área de texto arroja un error en Chrome/Firefox pero no en Safari

Tengo un área de texto que se expande dinámicamente para adaptarse al contenido ingresado. Funciona correctamente en todos los navegadores, por lo que puedo decir, pero en FireFox y Chrome da este error en la consola:

Uncaught TypeError: Cannot set properties of undefined (setting 'height') at oninput

Aquí está el código con un ejemplo:

 let ta = document.getElementsByTagName("textarea"); for (let i = 0; i < ta.length; i++) { ta[i].setAttribute("style", "min-height:" + (ta[i].scrollHeight) + "px;overflow-y:hidden;"); ta[i].addEventListener("input", oninput, false); function oninput() { this.style.height = "auto"; let maxheight = 200; if (this.scrollHeight < maxheight){ this.style.height = (this.scrollHeight) + "px"; }else{ this.style.height = maxheight + "px"; } } }
 <textarea>type here...</textarea> <br><br> <input type="text" placeholder="This should not be affected">

Me gustaría que este código funcione sin problemas en todas las plataformas. Además, quiero asegurarme de que solo estoy apuntando al área de textarea y no a input , ya que el error se presenta si también cambio una entrada normal.

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

0

Puede cambiar el nombre de la función con onInput o detener la propagación de eventos

 function oninput(evt) { evt.stopPropagation() // ... your code here }

El código que escribiste arriba se convertirá a continuación (supongo)

 let ta = document.getElementsByTagName("textarea"); for (let i = 0; i < ta.length; i++) { oninput = function() { this.style.height = "auto"; let maxheight = 200; if (this.scrollHeight < maxheight){ this.style.height = (this.scrollHeight) + "px"; }else{ this.style.height = maxheight + "px"; } ta[i].setAttribute("style", "min-height:" + (ta[i].scrollHeight) + "px;overflow-y:hidden;"); ta[i].addEventListener("input", oninput, false); } }

¿Por qué ocurre la conversión? Es una de las peculiaridades de javascript Hoisting

oninput = ... no tiene alcance, en modo no estricto está limitado a la ventana, será como window.oninput = xxx

Lo siento mi mal inglés

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!