insertar texto en la posicion del cursor dentro de un textArea
Hola a todos:
Me gustar铆a hacer una funci贸n que inserte un texto dentro del contenido de un textArea en el exacto lugar donde est茅 en ese momento el cursor del rat贸n.
Lo he conseguido si selecciono parte del contenido de la textArea, pero si no tengo nada seleccionado, o sea, simplemente tengo el cursor en un lugar de la textArea , no se como hacerlo.
El c贸digo que tengo es el siguiente:
____________________Inicio c贸digo
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>pruebaTextArea.html</TITLE>
<script type="text/javascript">
<!--
function overwritePaste(pContPP){
//alert("inicio de overwritePaste()");
//var pContPP = window.clipboardData.getData("Text");
var sel = document.selection ;
var fuente = event.srcElement;
var desde=event.fromElement;
var okDato = true;
var txta=document.ta;
alert("sel="+sel.text+"-fuente="+fuente.name+"-okDato="+okDato+"-desde="+desde);
if (okDato) {
if (sel!=null) {
alert("sel distinto de null");
var rng = sel.createRange();
alert("rng="+rng.text);
if (rng!=null) {
alert("rng distinto de null: rng.text="+rng.text);
rng.text=pContPP;
alert("he ejecutado rng.text=pContPP="+pContPP);
} else {
alert("sel = null");
//event.srcElement.value = pContPP;
txta.value=pContPP;
alert("he ejecutado txta.value=pContPP="+pContPP);
}
} else {
//event.srcElement.value = pContPP;
txta.value=pContPP;
alert("he ejecutado txta.value=pContPP="+pContPP);
}
ctrlObligario(event.srcElement);
}
return false;
}
//-->
</script>
</HEAD>
<BODY>
<FORM>
<TEXTAREA rows="5" cols="20" name="ta">
</TEXTAREA>
<INPUT type="button" name="boton" value="Incluir Texto en TextArea" onclick="overwritePaste('Texto Nuevo')">
</FORM>
</BODY>
</HTML>
____________________Fin c贸digo
驴alguien puede ayudarme?
Me gustar铆a hacer una funci贸n que inserte un texto dentro del contenido de un textArea en el exacto lugar donde est茅 en ese momento el cursor del rat贸n.
Lo he conseguido si selecciono parte del contenido de la textArea, pero si no tengo nada seleccionado, o sea, simplemente tengo el cursor en un lugar de la textArea , no se como hacerlo.
El c贸digo que tengo es el siguiente:
____________________Inicio c贸digo
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>pruebaTextArea.html</TITLE>
<script type="text/javascript">
<!--
function overwritePaste(pContPP){
//alert("inicio de overwritePaste()");
//var pContPP = window.clipboardData.getData("Text");
var sel = document.selection ;
var fuente = event.srcElement;
var desde=event.fromElement;
var okDato = true;
var txta=document.ta;
alert("sel="+sel.text+"-fuente="+fuente.name+"-okDato="+okDato+"-desde="+desde);
if (okDato) {
if (sel!=null) {
alert("sel distinto de null");
var rng = sel.createRange();
alert("rng="+rng.text);
if (rng!=null) {
alert("rng distinto de null: rng.text="+rng.text);
rng.text=pContPP;
alert("he ejecutado rng.text=pContPP="+pContPP);
} else {
alert("sel = null");
//event.srcElement.value = pContPP;
txta.value=pContPP;
alert("he ejecutado txta.value=pContPP="+pContPP);
}
} else {
//event.srcElement.value = pContPP;
txta.value=pContPP;
alert("he ejecutado txta.value=pContPP="+pContPP);
}
ctrlObligario(event.srcElement);
}
return false;
}
//-->
</script>
</HEAD>
<BODY>
<FORM>
<TEXTAREA rows="5" cols="20" name="ta">
</TEXTAREA>
<INPUT type="button" name="boton" value="Incluir Texto en TextArea" onclick="overwritePaste('Texto Nuevo')">
</FORM>
</BODY>
</HTML>
____________________Fin c贸digo
驴alguien puede ayudarme?
