WebFX WebBoard
Message Thread:
View All Messages
Back to WebFX
 preserving caret position i n content editable div, adam, September 3, 2003
     Re:preserving caret position i n content editable div, JT_Kiwi, September 5, 2003
         Re:preserving caret position i n content editable div, adam, September 5, 2003
             Re:preserving caret position in contentEditable div, Chris, September 5, 2003
             Re:preserving caret position in contentEditable div, Chris, September 5, 2003

Subject: Re:preserving caret position i n content editable div From: JT_Kiwi Date: September 5, 2003

Ok the below works but this key catching stuff can get messy pretty quickly

<script language="JavaScript">
<!--
 function divOnKeyDown() {
 var code = event.keyCode;
 var sel = document.selection.createRange()
 var color = sel.queryCommandValue('forecolor')
 if(code >= 48 && code <= 57 && color != 255){
  sel.pasteHTML('<font color="red">'+(code-48)+'</font>')
  return false
 }
}

//-->
</script>
<div contentEditable="true" onkeydown="return divOnKeyDown() ">Test Red Key</div>


 

From: adam
Sent: September 4, 2003
Subject: Re:preserving caret position i n content editable div

Hi,

I'm trying to have a contentEditable div which automatically searches for the first instance of a number and colours it red. Im trapping the onKeyDown event to do this. It works ok, except that the caret position is always put to the end of the field which is not ok if you type in the middle of it. Here is the code I am using on the keyDown event. I just can't get it to work so that it restores the original caret position.

 function divOnKeyDown() {

         var srcEl = event.srcElement;

         if(srcEl){

             srcEl.rng = document.selection.createRange().duplicate();

             var sNum = getNumber(srcEl.innerText);

             srcEl.innerHTML = srcEl.innerText.replace(sNum, "" + sNum + "");

             srcEl.rng.collapse(false);

             srcEl.rng.select();

             srcEl.focus();

        }

}

What obvious thing am I doing wrong?

Adam





Reply
Name: 
Email: 
Subject: 

Enter your reply to this message below. HTML tags are not supported but words that start with http://, ftp:// or mailto: are converted to links.


View All Messages
Back to WebFX