proee
asked on
input box scroll to end of value with javascript
I have an html input box of type="text" that has a value assigned to it that exceeds the display width. My goal is to have some javascript that can grab focus of the input box and scroll the cursor to the end of the input box value and allow the user to append to the current value. I have used the following code to position the character at the end of the input box value and this seems to work ok in IE. However, in FF and Safari, only the cursor moves to the specified location and the text inside the input box does not track to the new position. Is there a way I can have the input box show the end of the string (similar to using the righ-arrow key on the keyboard to scroll to the end of the last character in the input box)?
function setCaretTo(pos) {
var obj=document.getElementById('myInputBox');
if(obj.createTextRange) {
/* Create a TextRange, set the internal pointer to
a specified position and show the cursor at this
position
*/
var range = obj.createTextRange();
range.move("character", pos);
range.select();
} else if(obj.selectionStart) {
/* Gecko is a little bit shorter on that. Simply
focus the element and set the selection to a
specified position
*/
obj.focus();
obj.setSelectionRange(pos, pos);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
upon further research, it appears the above error is related to something in FF which is going to be fixed in version 3. The only fix is a hack that is to put and autocomlete="off" inside the input tag. This however, is not w3C compliant html code but prevent the javascript error. Choose your poison!
ASKER
Thanks a million.
ASKER
[Exception... "'Permission denied to set property XULElement.selectedIndex' when calling method: [nsIAutoCompletePopup::sel
[Break on this error] obj.dispatchEvent(evt);
Any ideas on why this error is happening?
Also, in Safari for windows, it works but only after running the function two times.
Opera = does not work (that's ok, but curious if initKeyEvent works for Opera).