Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

23 of 35 people (66%) answered Yes
Recently 7 of 10 people (70%) answered Yes


How can I find out the position (similar to indexOf()) of the cursor in a textarea ?

May 14th, 2009 12:27
tikane tinjane, ha mo, Gunnar Liljas, Neil Hardie, Andy Maurer,

Here's a little function I just came up with. 
function caretPos(textEl)
		var i=textEl.value.length+1;
		if (textEl.createTextRange)
			theCaret = document.selection.createRange
			while (theCaret.parentElement()==textEl && 
theCaret.move("character",1)==1) --i;
			return i;
		else return -1;