Jump to content

Recommended Posts

The weirdest thing is happening. I'm using this function someone else made

getCaretPosition

which retrieves the position of the cursor in a textarea. I only use this function for IE. The problem is that the

getCaretPosition

function seems to return the length of the textarea-value. So if you have the text "abcd" in the textarea (with the cursor between the 'b' and 'c') and press the link, the result will be "ab%%%%%%cd" with the cursor set after the first 4 characters (in this case "ab%%"). The way I have the javascript set up right now, the cursor should not move.

 

Am I missing something?

<html>
<head>
	<script type="text/javascript">
		function insert_formatText(pre, suf, fid, eid){
			if(document.selection){ //Internet Explorer
				var str = document.selection.createRange().text;
				document.forms[fid].elements[eid].focus();
				var sel = document.selection.createRange();
				var caretPos = getCaretPosition(document.forms[fid].elements[eid]);
				sel.text = pre + str + suf;
				setCaretPosition(document.forms[fid].elements[eid], caretPos);
				return;
			} else if ((typeof document.forms[fid].elements[eid].selectionStart) != 'undefined'){ //Firefox
				var txtarea = document.forms[fid].elements[eid];
				var selLength = txtarea.textLength;
				var selStart = txtarea.selectionStart;
				var selEnd = txtarea.selectionEnd;
				var oldScrollTop = txtarea.scrollTop;
				var s1 = (txtarea.value).substring(0, selStart);
				var s2 = (txtarea.value).substring(selStart, selEnd)
				var s3 = (txtarea.value).substring(selEnd, selLength);
				txtarea.value = s1 + pre + s2 + suf + s3;
				//set the cursor in-between the pre and suf parameters
				var pos = (s1.length + pre.length);
				txtarea.focus();
				txtarea.setSelectionRange(pos, pos);
				txtarea.scrollTop = oldScrollTop;
				return;
			} else
				document.forms[fid].elements[eid].value += pre + suf;
		}
		function setCaretPosition(ctrl, pos){
			if(ctrl.setSelectionRange){
				ctrl.focus();
				ctrl.setSelectionRange(pos,pos);
			} else if (ctrl.createTextRange) {
				var range = ctrl.createTextRange();
				range.collapse(true);
				range.moveEnd('character', pos);
				range.moveStart('character', pos);
				range.select();
			}
		}

		function getCaretPosition(ctrl){
			var CaretPos = 0;
			if(document.selection){ // IE Support
				ctrl.focus ();
				var Sel = document.selection.createRange ();
				Sel.moveStart ('character', -ctrl.value.length);
				CaretPos = Sel.text.length;
			} else if (ctrl.selectionStart || ctrl.selectionStart == '0') // Firefox support
				CaretPos = ctrl.selectionStart;
			return (CaretPos);
		}
	</script>
</head>
<body>
	<form id="entryform">
		<a href="javascript:void(0);" onclick="insert_formatText('%%%', '%%%', 'entryform', 'text');">Bold</a><br />
		<textarea id="text"></textarea>
	</form>
</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.