SoloCodigo

Programación Web y Scripting => JavaScript => Mensaje iniciado por: el_shanky en Miércoles 11 de Junio de 2008, 08:25

Título: Insertar Texto En Un Textarea
Publicado por: el_shanky en Miércoles 11 de Junio de 2008, 08:25
Puntualmente eso... necesito saber como insertar texto que tengo en una variable javascript, en la posición donde se encuentra el cursor en un textarea...

No me suena muy complicado y mas para los problemas que ya he superado desde que empezé con esto (javascript) siendo que soy un total newbie... PERO ESTO ME PUEDE!!!!

Spero su ayuda! desde ya muchas gracias...
Título: Re: Insertar Texto En Un Textarea
Publicado por: el_shanky en Jueves 12 de Junio de 2008, 06:26
Al final lo solucioné al problema! pongo acá la funcion x si alguien le ahce falta...

NOTA: La funcion es una que modifiqué que encontré x ahí!


Código: Text
  1. function insertafoto(newfoto){
  2.  
  3. newfoto="../../fotos/mediana/"+newfoto;//creo el path de la foto selecionada
  4.  
  5. dato='<br><br><table width=600 cellspacing=0><tr><th scope=col><img src='+newfoto+'></th><th scope=col> </th></tr></table><br>';
  6.  
  7. //window.opener.document.noticias.texto.value+=dato; //el .document. es fundamental para firefox! sinó no anda!
  8.  
  9.  
  10. var aTag =  dato;
  11. var eTag =  " ";
  12. var input = window.opener.document.noticias.texto;//el .document. es fundamental para firefox! sinó no anda!
  13. input.focus();
  14.  
  15. //FOR IE
  16. if  (typeof document.selection != 'undefined') {
  17.    
  18.     var range = window.opener.document.selection.createRange();
  19.     var insText = range.text;    
  20.     range.text = aTag + insText + eTag;
  21.     range = window.opener.document.selection.createRange();
  22.   if  (insText.length == 0) {
  23.       range.move('character', -eTag.length);
  24.   }  else {
  25.       range.moveStart('character', aTag.length + insText.length + eTag.length);      
  26.    }
  27.    range.select();
  28.  
  29. }   else  {  
  30.  
  31. // FOR Gecko Browser
  32.  
  33. if  (typeof input.selectionStart != 'undefined') {
  34.  
  35.   var start = input.selectionStart;
  36.   var end = input.selectionEnd;
  37.   var insText = input.value.substring(start, end);
  38.   input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
  39.  
  40.   var pos;
  41.   if  (insText.length == 0) {
  42.     pos = start + aTag.length;
  43.   } else {
  44.     pos = start + aTag.length + insText.length + eTag.length;
  45.   }
  46.    input.selectionStart = pos;
  47.    input.selectionEnd = pos;
  48.  
  49.   }
  50. }
  51.  
  52.  
  53. self.close();
  54. }//end function
  55.  
  56.  

salute!
Título: Re: Insertar Texto En Un Textarea
Publicado por: el_shanky en Sábado 12 de Julio de 2008, 06:43
ES LA SEGUNDA VEZ QUE ME PASA QUE TENGO UNA DUDA BUSCO INFORMACIÓN Y ENCUENTRO UN POST MIO VIEJO CON LA SOLUCION... SI LO C TENGO PROBLEMAS SERIOS DE MEMORIA...

TIP PARA MI: Usar la misma funcion de arriba unicamente pasandole los parametros de un solo lado si no quiero que encierre contenido...