Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textarea It doesnt retrieve the caret position when new line

Status
Not open for further replies.

edfimasa

Programmer
Apr 9, 2008
21
0
0
PT
Hi.

I found the following function to retrieve the caret of a textarea:

function doGetCaretPosition (ctrl) {

var CaretPos = 0;
// IE Support
if (document.selection) {

ctrl.focus ();
var Sel = document.selection.createRange ();

Sel.moveStart ('character', -ctrl.value.length);

CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart;

return (CaretPos);

}

It works fine(althow the textarea needs to be the first visible element after the body), but when i try to retrieve the caret (when its in a new blank line), it doesnt give me the position of the new line, but the end of the previews one.

Example.

This is the text in
my text area.

If i press enter (new line) after area. it will give me the position just after area. and not the new line.

Can you help?
Cheers.
 
It's counting characters by the looks of things and \r or \n are not characters, you need to included these in your calculation.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top