[Javascript] Get the position of the cursor in a textarea?

Chris Tifer christ at saeweb.com
Tue Jun 24 07:46:18 CDT 2003


Actually, it is:
=========================

<script language="javascript">
<!--

 function emoticon(strEmoticon) {
  var objMailForm = document.forms['form1']
  var objEl = objMailForm.elements['message']
  insertAtCaret(strEmoticon)
  document.form1.message.focus();

 }

//-->
</script>


<SCRIPT language="javascript">
 function storeCaret(){
  var objMailForm = document.forms['form1']
  var objEl = objMailForm.elements['message']
  if (objEl.createTextRange){
   objEl.caretPos = document.selection.createRange().duplicate();
  }
 }


 function insertAtCaret(text){
  var objMailForm = document.forms['form1']
  var objEl = objMailForm.elements['message']
  if (objEl.createTextRange && objEl.caretPos){
   var caretPos = objEl.caretPos;
   caretPos.text =
   caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' :
text;
  }
  else{
   objEl.value += text + " ";
  }
 }
</SCRIPT>
=========================


The insertAtCaret function is the one that seems to find
the position of the insertion point and uses that to place
text. This script is rather cryptic and there's not much documentation,
but I've seen it all around - even on Microsoft's site.

You can also find it on some forums that allow emoticons
as that determines where the emoticon can get entered...

Chris Tifer


----- Original Message -----
From: "Peter-Paul Koch" <gassinaumasis at hotmail.com>
To: <javascript at LaTech.edu>
Sent: Tuesday, June 24, 2003 7:31 AM
Subject: Re: [Javascript] Get the position of the cursor in a textarea?


>
>
> > > > Hi all.  Is it possible to get the position of the cursor
> > > in a textarea?
>
> No. Unfortunately that's not possible.
>
> -------------------------------------------------------------------
> ppk, freelance web developer
> Interactie, copywriting, JavaScript, integration
> http://www.xs4all.nl/~ppk/
> Column "Keep it Simple": http://www.digital-web.com/columns/keepitsimple/
> New: Forms, usability and the W3C DOM:
> http://digital-web.com/features/feature_2003-05b.shtml
> ------------------------------------------------------------------
>
> _________________________________________________________________
> Tired of spam? Get advanced junk mail protection with MSN 8.
> http://join.msn.com/?page=features/junkmail
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript



More information about the Javascript mailing list