Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Placing a cursor within a body of text


Message #1 by "Robert Adams" <rob@e...> on Wed, 18 Sep 2002 14:43:25
I've been working on the whole contenteditable/creatrange/textrange in 
order to create a rich-text editor.  I've been pretty successful, able to 
put in tab, colors, bolds, blah-blah.

But, backspaces presented a problem for me (pressing the backspace key did 
nothing).  So, using the following code i worked something out (partial):

var intCode = event.keyCode;

switch (intCode)
	{
	case 8:	//backpace
	elem.caretPos = document.selection.createRange();	
	
	var marc = '~';
	var caretPos = elem.caretPos;
					  
	caretPos.text = marc;
	
	var orig = elem.innerHTML;
		
	var i = orig.indexOf(marc);
	var lng = orig.length;
							  
	strStart = orig.slice(0,(i-1));
	strEnd = orig.slice((i+1),(lng));

	elem.innerHTML = strStart + strEnd;
	break;
-----
Now, it does work (and quickly, too), but here's the problem.  If the user 
places their cursor somewhere *before* the end of the text's line and then 
hits backspace, it does backspace, but it then places the cursor to the 
very end of the line of text.  Not too handy.  So, my question...

Is there a way to specify a cursors location within a body of text?  For 
example, could i somehow place the cursor after the 18th character in a 
body of text?  Or, is there a better way to get backspaces working when 
doing contenteditable text editing ???

.rob
rob@e...


  Return to Index