1

I just use js to set an text input's value. But when the text is longer than input can hold, the excess text is hidden in the right part of the input. How can I hide the excess text in left area just like the normal typing? Forgive my poor english.

2
  • Why do you want to hide it from left side? It's supposed to hide automatically on value change. Maybe you've got select() or focus()? Can you show your code? Commented May 14, 2013 at 6:01
  • I think I understand what he’s asking, and can provide an example. When he sets the value of a text field to “abc[…]xyz” with JS, the text field currently looks like [abcdefg] on the page. He wants it to look like [tuvwxyz], the same way it would look if the user typed “abc[…]xyz” out with their keyboard. Commented May 14, 2013 at 6:08

1 Answer 1

1

When you perform this your caret position is at the beginning of the text. You need to move the caret position to the end.

function setCursor(node,pos){
    var node = (typeof node == "string" || node instanceof String) ? document.getElementById(node) : node;

    if (!node) {
        return false;
    } else if (node.createTextRange) {
        var textRange = node.createTextRange();
        textRange.collapse(true);
        textRange.moveEnd(pos);
        textRange.moveStart(pos);
        textRange.select();
        return true;
    } else if (node.setSelectionRange) {
        node.setSelectionRange(pos,pos);
        return true;
    }
    return false;
}

setCursor(input, input.value.length); // input is your textbox
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.