Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 660
Uwe Keim Avatar asked Oct 25 '11 16:10

Uwe Keim


1 Answers

You could try triggering a right key press after focus.

$('#textbox').focus();
var e = jQuery.Event("keydown");
e.which = 39; // aka right arrow.
$("input").trigger(e);
like image 96
Jake Cattrall Avatar answered Sep 20 '22 19:09

Jake Cattrall