ornerylawn
ornerylawn

Reputation: 524

IE textarea selectionStart is wrong after setting value containing newline

If I type a and then <enter> into an IE11 textarea and log selectionStart, it's 2 (which is what I would expect). However, if I set the value of the textarea programmatically to 'a\n' instead, selectionStart is 4.

$('textarea').val('a\n');
console.log($('textarea').get(0).selectionStart);

Is there anyway to get an accurate cursor position here?

Upvotes: 3

Views: 739

Answers (1)

Superole
Superole

Reputation: 1371

This is because in IE the cursor doesn't move unless the textarea has been given focus first.

Try

$('textarea').focus().val('a\n');
console.log($('textarea').get(0).selectionStart);

Upvotes: 1

Related Questions