Ver Mensaje Individual
  #2 (permalink)  
Antiguo 14/11/2013, 06:40
Avatar de Maganius
Maganius
 
Fecha de Ingreso: septiembre-2010
Mensajes: 310
Antigüedad: 13 años, 7 meses
Puntos: 10
Respuesta: [APORTE] Sencilla función en jquery para que el textarea se ajuste al text

Gracias por el aporte! seguramente a muchos le servirá.

Tal vez quede mejor así para no tener que pasar un ID siempre

Código HTML:
Ver original
  1. <textarea cols="60" style="resize:none; overflow:hidden; width: 250px; height:80px;" onKeyUp="controlTextArea(80);"></textarea>

Código Javascript:
Ver original
  1. function controlTextArea(altInicial){
  2.     var altoExt = $(this).height();
  3.     var altoInt = $(this).scrollHeight;
  4.     if(altoInt > altoExt){
  5.         $(this).animate({
  6.             height: altoInt+"px"
  7.         },"fast");
  8.     }else{
  9.         $(this).css({
  10.             height: altInicial+"px"
  11.         });
  12.         if(altoInt > altInicial){
  13.             $(this).css({
  14.                 height: altoInt+"px"
  15.             });
  16.         }
  17.     }
  18. }

No entiendo porque en el ELSE haces la misma comprobación que en el IF? no lo probe por lo que estaba en duda si borrarlo o no.

Última edición por Maganius; 14/11/2013 a las 06:49