Foros del Web » Programando para Internet » Javascript »

Reemplazar Enter por <br> en un Textarea

Estas en el tema de Reemplazar Enter por <br> en un Textarea en el foro de Javascript en Foros del Web. Junto con saludarlos y molestarlos me dirijo a uds. para hacer la siguiente consulta como puedo hacer cuando alguien aprete enter en un <textarea></textarea> lo ...
  #1 (permalink)  
Antiguo 20/10/2005, 09:53
 
Fecha de Ingreso: abril-2005
Mensajes: 55
Antigüedad: 19 años
Puntos: 0
Reemplazar Enter por <br> en un Textarea

Junto con saludarlos y molestarlos me dirijo a uds. para hacer la siguiente consulta como puedo hacer cuando alguien aprete enter en un <textarea></textarea> lo reemplaze al momento de enviar por un <br> y haci poder guadarlo en la db y no tener todo el texto junto, de ante mano muchas gracias por las ayudas prestadas.

Saludos Cordiales,
  #2 (permalink)  
Antiguo 20/10/2005, 10:04
Avatar de Vaalegk  
Fecha de Ingreso: abril-2005
Mensajes: 154
Antigüedad: 19 años
Puntos: 2
eso lo manejarias con el lenguaje que uses para procesar los datos, si es php
puedes usar nltobr() que te convierte los saltos de linea en <br> en asp
tendrias que hacer un replace del chr(13) (no me acuerdo bien)
  #3 (permalink)  
Antiguo 20/10/2005, 10:05
Avatar de Saruman  
Fecha de Ingreso: mayo-2003
Ubicación: Panama city, Panama, Panama
Mensajes: 1.154
Antigüedad: 21 años
Puntos: 5
ok, tengo esto... pero funciona bien cuando quieres cambiar de <br> a enters, no viceversa... y entonces esta es mi pregunta para los foreros: como hago para reemplazar en todo el contenido el chr(13) a <br> globalmente?? asi como lo tengo solo me reemplaza el de la primera linea... esto lo he buscado por todos lados y no c....

saludos

Código HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<script language="javascript">
function ReplaceEnters(texto, conbr) {
 if (conbr) {
  texto = texto.replace(String.fromCharCode(13), "<br>");
 } else {
  texto = texto.replace(/<br>/g, String.fromCharCode(13));
 }
 alert(texto);
}
</script>
<body>
<textarea name="test" id="test" rows="10" cols="50"></textarea>
<input type="button" value="ok" onclick="ReplaceEnters(document.getElementById('test').value, true)"/>
</body>
</html> 
__________________
Saruman

One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them.
  #4 (permalink)  
Antiguo 20/10/2005, 10:27
Avatar de Vaalegk  
Fecha de Ingreso: abril-2005
Mensajes: 154
Antigüedad: 19 años
Puntos: 2
asi?:
Código HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<script language="javascript">
function ReplaceEnters(texto, conbr) {
 //para que funcione en IE y Firefox
 var nl=document.all?String.fromCharCode(13):"\n";
 if (conbr) { 	
 	while(texto.indexOf(nl)>=0){
	  texto = texto.replace(nl, "<br/>");
	  /*en IE aunque remplaces los char(13) los saltos se mantienen, debes hacer esto*/
	  if(document.all)  texto = texto.replace("\n", "");
	}
 } else {
	while(texto.indexOf("<br/>")>=0){
	  texto = texto.replace("<br/>",nl);
	}
 }
 return texto;
}
</script>
<body>
<textarea name="test" id="test" rows="10" cols="50"></textarea>
<input type="button" value="code" onclick="document.getElementById('test').value=ReplaceEnters(document.getElementById('test').value, true)"/>
<input type="button" value="decode" onclick="document.getElementById('test').value=ReplaceEnters(document.getElementById('test').value, false)"/>

</body>
</html> 
  #5 (permalink)  
Antiguo 20/10/2005, 10:37
Avatar de Saruman  
Fecha de Ingreso: mayo-2003
Ubicación: Panama city, Panama, Panama
Mensajes: 1.154
Antigüedad: 21 años
Puntos: 5
asi mismo Vaalegk
gracias Carlos
__________________
Saruman

One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them.
  #6 (permalink)  
Antiguo 20/10/2005, 11:44
Avatar de tunait
Moderadora
 
Fecha de Ingreso: agosto-2001
Ubicación: Terok Nor
Mensajes: 16.805
Antigüedad: 22 años, 8 meses
Puntos: 381
Buenas a todos

Saruman, se puede simplificar usando el String.fromCharCode(13) como expresión regular de la siguiente forma
Código:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<script language="javascript">
function ReplaceEnters(texto, conbr) {
 if (conbr) {
 pat = new RegExp(String.fromCharCode(13),"g")
  texto = texto.replace(pat, "<br>");
 } else {
  texto = texto.replace(/<br>/g, String.fromCharCode(13));
 }
 alert(texto);
}
</script>
<body>
<textarea name="test" id="test" rows="10" cols="50"></textarea>
<input type="button" value="ok" onclick="ReplaceEnters(document.getElementById('test').value, true)"/>
</body>
</html>
Saludos
  #7 (permalink)  
Antiguo 20/10/2005, 11:56
Avatar de tunait
Moderadora
 
Fecha de Ingreso: agosto-2001
Ubicación: Terok Nor
Mensajes: 16.805
Antigüedad: 22 años, 8 meses
Puntos: 381
...por cierto....

Que con cada intro explorer (Opera también) recoge dos códigos, un String.fromCharCode(10) y un String.fromCharCode(13), y mozilla sólo recoge el String.fromCharCode(10).

Para que devuelvan idénticos resultados se le puede pedir así

Código:
<script language="javascript">
function ReplaceEnters(texto, conbr) {
 if (conbr) {
 pat = new RegExp(String.fromCharCode(13),"g")
 pat2 = new RegExp(String.fromCharCode(10),"g")
  texto = texto.replace(pat, "");
   texto = texto.replace(pat2, "<br>");
 } else {
  texto = texto.replace(/<br>/g, String.fromCharCode(13));
 }
 alert(texto);
 return texto
}
</script>
  #8 (permalink)  
Antiguo 20/10/2005, 12:11
Avatar de Saruman  
Fecha de Ingreso: mayo-2003
Ubicación: Panama city, Panama, Panama
Mensajes: 1.154
Antigüedad: 21 años
Puntos: 5
super cool, gracias!!!!
__________________
Saruman

One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Tema Cerrado




La zona horaria es GMT -6. Ahora son las 08:03.