Foros del Web » Programando para Internet » Javascript »

Capturar la posicion cuando hago click en un textarea

Estas en el tema de Capturar la posicion cuando hago click en un textarea en el foro de Javascript en Foros del Web. Hola, en un textarea tengo un texto un frase digamos, y cuando quisiera que cuando haga click en algun lugar de la frase me diga ...
  #1 (permalink)  
Antiguo 10/07/2009, 19:51
 
Fecha de Ingreso: diciembre-2006
Mensajes: 381
Antigüedad: 17 años, 4 meses
Puntos: 2
Capturar la posicion cuando hago click en un textarea

Hola, en un textarea tengo un texto un frase digamos, y cuando quisiera que cuando haga click en algun lugar de la frase me diga cuentos caracteres van escritos hasta esa posicion donde estaria el cursor, estoy tratando pero solo puedo capturar la longitud total del texto en un textarea con el length.

Pueden ayudarme?
  #2 (permalink)  
Antiguo 10/07/2009, 20:06
Avatar de duskrow  
Fecha de Ingreso: abril-2008
Mensajes: 267
Antigüedad: 16 años, 1 mes
Puntos: 8
Respuesta: Capturar la posicion cuando hago click en un textarea

Cita:
function contador_text(){
var __permitido = 114 ;
var __texto_permitido = '' ;
var __texto = $("#texto").attr("value") ;
var __total = __permitido - __texto.length ;
var __array = new Array();
__array = __texto.split('');


for(i=0 ; i<__texto.length ; i++){

if( i <= __permitido){
__texto_permitido += __array[i] ;
}

}

$("#texto").html(__texto_permitido) ;

if(__total >= 0){
$("#faltantes").html(__total) ;
}
return false ;
}

es una funcion que desarrolle hace rato , requieres jquery
  #3 (permalink)  
Antiguo 10/07/2009, 20:22
 
Fecha de Ingreso: diciembre-2006
Mensajes: 381
Antigüedad: 17 años, 4 meses
Puntos: 2
Respuesta: Capturar la posicion cuando hago click en un textarea

umm, creo que esa funcion no es para lo que necesito, osea cuando haga click en el textarea, que cuente cuantos caracteres hay desde el inicio del textarea hasta donde esta el cursor donde hize click
  #4 (permalink)  
Antiguo 10/07/2009, 21:56
Avatar de DeveloperFresh  
Fecha de Ingreso: mayo-2009
Ubicación: Ancon City
Mensajes: 35
Antigüedad: 15 años
Puntos: 3
Respuesta: Capturar la posicion cuando hago click en un textarea

Talvez esto te ayude!

Código HTML:
<html>
<head>
  <title></title>
<script>
function contar(ele){
if(typeof ele.selectionStart != 'undefined')
alert(ele.selectionStart);
}
</script>
</head>
    <body>
<textarea rows="8" cols="30" onclick="contar(this)">Un conjunto de frases</textarea>
</body>
</html> 
  #5 (permalink)  
Antiguo 10/07/2009, 23:56
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: Capturar la posicion cuando hago click en un textarea

Un ejemplo para textarea e input (explorer obliga a usar caminos diferentes):
Código:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ejemplo</title>
<script>
function devPos(input){
if(typeof document.selection != 'undefined' && document.selection && typeof input.selectionStart == 'undefined' || input.selectionStart=='vacio'){
var str =document.selection.createRange();
var stored_range = str.duplicate();
stored_range.moveToElementText(input);
stored_range.setEndPoint( 'EndToEnd', str );
input.selectionStart = stored_range.text.length - str.text.length;
input.selectionEnd = input.selectionStart + str.text.length;
alert(input.selectionStart);
input.selectionStart='vacio';
}else if(typeof input.selectionStart != 'undefined'){
alert(input.selectionStart);
}
}
function devPos2(input){
if(typeof document.selection != 'undefined' && document.selection && typeof input.selectionStart == 'undefined'){
var range = document.selection.createRange();
var bookmark = range.getBookmark();
var caret_pos = bookmark.charCodeAt(2) - 2;
alert(caret_pos)
}else if(typeof input.selectionStart != 'undefined'){
alert(input.selectionStart);
}
}

</script>
</head>

<body>
(click sobre los campos para obtener la posición del cursor)
<form id="form1" name="form1" method="post" action="">
  <textarea name="area" rows="1" id="area" onclick="devPos(this)">esta es una prueba</textarea>
  
  <br />

  <input name="campo" type="text" id="campo" value="ver qu&eacute; pasa en textfield" size="60" onclick="devPos2(this)" />
</form>
</body>

</html>
Más info:
http://www.disegnocentell.com.ar/notas2.php?id=206
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.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:29.