18-Mar-06 (Created: 18-Mar-06) | More in 'CS-JavaScript'

js: How to get a cursor position in a text field

function getCursorPositionInTextField(textFieldObj) 
	//This is required if you were to be clicking on a button to invoke this
	//otherwise your selection is the button
	textrange = document.selection.createRange().duplicate();
	//You may have to make sure the selection and focus is with your object
	//because the selection is document level entity.
	//if (textrange.parentElement() != textFieldObj)
	//	alert("Sorry you are not the field");
	var i=0;
	while(textrange.move("character",1) == 1)
	return textFieldObj.value.length() - i;

>> Saturday, March 18, 2006 10:32:42 AM - Comments by satya

The above code works only for ie