I have an image selector that allows to choose an image from a gallery, then fills in the URL into a <input type="text">
field. The URLs can be awfully long, and always seeing the first half of the URL in the text field has very little informational value.
Does somebody know a way to "scroll" to the very right of the text field so that the end of the URL is visible instead of the beginning? Without resorting to a textarea.
1 Answer. Show activity on this post. If you're using an iPhone with Force Touch you can force press down anywhere on the keyboard to bring up the typing cursor. If you drag your finger to the right/left side of the display(without letting go) the text will scroll in that direction.
Use the href Property to Scroll to an Element in JavaScript href = "#"; location. href = "#myDiv"; Here #myDiv is the ID of the required <div> tag.
Scroll one page at a time in all major browsers including Microsoft Internet Explorer and Mozilla Firefox by pressing the Spacebar key. Move back up the page by pressing Shift + Spacebar or the Home key on the keyboard.
Set HTMLInputElement.setSelectionRange()
to the length of the input value after explicitly setting the focus()
. The disadvantage is that it scrolls back to start once blurred.
var foo = document.getElementById("foo");
foo.value = "http://stackoverflow.com/questions/1962168/scroll-to-the-very-right-of-a-long-text-input";
foo.focus();
foo.setSelectionRange(foo.value.length,foo.value.length);
<input id="foo">
If you don't care about IE in its entirety, then set Element.scrollLeft
to Element.scrollWidth
. The disadvantage is the less browser support.
var foo = document.getElementById("foo");
foo.value = "http://stackoverflow.com/questions/1962168/scroll-to-the-very-right-of-a-long-text-input";
foo.scrollLeft = foo.scrollWidth;
<input id="foo">
If you'd like to support every single browser, consider to trick it with the dir
(direction) attribute which you set to rtl
(right-to-left). The disadvantage is that it's a hack which really need to be taken into consideration when it's editable and/or you develop a direction sensitive website, but this works on all browsers and is great on readonly inputs.
var foo = document.getElementById("foo");
foo.value = "http://stackoverflow.com/questions/1962168/scroll-to-the-very-right-of-a-long-text-input";
<input id="foo" dir="rtl">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With