Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

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

Entry

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
().duplicate();
			while (theCaret.parentElement()==textEl && 
theCaret.move("character",1)==1) --i;
			return i;
		}
		else return -1;
	}
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com
http://antworten.leblogguide.com/