Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_objects thread: cursor position within a textfield


Message #1 by "Fabian Göhlich" <fabian.goehlich@b...> on Thu, 16 Aug 2001 14:13:03
Don't know if this will help, I found this sometime ago, don't remember 
where but maybe it will get you in the right direction.

<html>
<head>
<title></title>
<SCRIPT>
function outPutArea (ta_text)
{
	document.write(ta_text);
}


function storeCaret (textEl) 
{
	if (textEl.createTextRange) 
	{
    	textEl.caretPos = document.selection.createRange().duplicate();
	}
}


function insertAtCaret (textEl, text) 
{
	text = "<b>" + text + "</b>";
    if (textEl.createTextRange && textEl.caretPos) 
	{
		var caretPos = textEl.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) 
== ' ' ? text + ' ' : text;
    }
    else
	{
    	textEl.value  = text;
	}     
}

function insertAtCaretLink (textEl, URL, display) 
{
	text = "<a href=\"" + URL + "\">" + display + "</a>";
    if (textEl.createTextRange && textEl.caretPos) 
	{
		var caretPos = textEl.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) 
== ' ' ? text + ' ' : text;
    }
    else
	{
    	textEl.value  = text;
	}     
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="aForm">
<TEXTAREA NAME="Content" ROWS="10" COLS="80" WRAP="soft"
               ONSELECT="storeCaret(this);"
               ONCLICK="storeCaret(this);"
               ONKEYUP="storeCaret(this);"></TEXTAREA>
<BR>
<INPUT TYPE="text" NAME="bold" SIZE="80" VALUE="">
    
<INPUT TYPE="button" VALUE="Bold Text"
	ONCLICK="insertAtCaret(this.form.Content, 
this.form.bold.value);"><br>
<br>
Text to appear as link: <INPUT TYPE="text" NAME="linkDisplay" SIZE="80" 
VALUE="">
Website address: <INPUT TYPE="text" NAME="linkURL" SIZE="80" 
VALUE="http://">
<INPUT TYPE="button" VALUE="Insert Link"
	ONCLICK="insertAtCaretLink(this.form.Content, 
this.form.linkURL.value, this.form.linkDisplay.value);"><br>


<input type="button" value="output" onClick="outPutArea
(this.form.Content.value);">

</FORM>
</body>
</html>

> I'm trying to find out where the user has placed the cursor within a 
> textfield. Position means the number of characters of the text in the 
> textfield. Must only work in ie5. Please help!

  Return to Index