Alex Rivera | Logout

Possible to scroll caret into view inside a single HTML text field with JavaScript?

Asked 2011-10-25T16:56:01.757
21

Knowing the jQuery Caret plugin, I'm still seeing no way to do the following on a single line text box in HTML (i.e. the input:type=text control) with JavaScript:

  1. Programmatically inserting text into a text box
  2. Setting the caret to the end.
  3. Making the caret visible (i.e. scroll the text box content)

I know how to do 1. and 2., just for 3. I have found no way of doing it.

To illustrate it:

enter image description here

My question is:

Is there any way to reliable put the caret at the end of the text and scroll it into the view?

(I can think of simulating pressing the END key or something like that, but I'm unsure whether this would be the best option).

Edit
Report

1 Answer

2

//hack to get cursor at end:

$(textboxselector).val($(textboxselector).val());

edit: should note focus first:

$(textboxselector).val(newtext);
$(textboxselector).focus();
$(textboxselector).val($(textboxselector).val());
answered 2011-10-26T13:45:47.247

Your Answer