19

I am trying to figure out how to highlight just some part of text inside an input box using jQuery. It's quite simple to highlight the entire contents of the input box but how do you highlight just one word or couple of letters?

Thanks!

3
  • By "select", do you mean mark it as selected? Blue, with white text? Commented Jun 21, 2010 at 14:30
  • 1
    What do you mean by select? Do you mean highlight like what happens when you select with your cursor? Commented Jun 21, 2010 at 14:31
  • I fixed the wording. I used the word select but what I really meant was highlight. I apologize for the mistake. Commented Jun 22, 2010 at 14:58

2 Answers 2

37

For text <input> elements, the following will do the job. The example selects just the word "two" in the input:

function setInputSelection(input, startPos, endPos) {
    input.focus();
    if (typeof input.selectionStart != "undefined") {
        input.selectionStart = startPos;
        input.selectionEnd = endPos;
    } else if (document.selection && document.selection.createRange) {
        // IE branch
        input.select();
        var range = document.selection.createRange();
        range.collapse(true);
        range.moveEnd("character", endPos);
        range.moveStart("character", startPos);
        range.select();
    }
}

document.getElementById("setSelection").onmousedown = function() {
    var input = document.getElementById("i");
    setInputSelection(input, 4, 7);
    return false;
};
<input id="i" type="text" value="One two three">
<input type="button" value="Set selection" id="setSelection">

Sign up to request clarification or add additional context in comments.

6 Comments

Honestly, I know this is not an educational comment, but come on IE, really!! 7 lines of code versus 2. Sure your way may add more functionality to the mix, but unnecessary. Thank you Tim for providing this example!
@JasonFoglia: The code for doing this reliably in old IE in textareas, where there could be empty lines, is much bigger still. In fairness, IE 9 fixes this by supporting selectionStart and selectionEnd.
I know this is old but Firefox requires this input.focus(); before input.selectionStart = startPos;
@BojanDević: Since textareas can have selections while not focussed in some browsers, I tend to try and keep the two things separate, but I think I agree with you for this question.
@vatavale: The main code will work fine for many years to come but the jsFiddle has issues with focus. I've changed it to use a code snippet instead and added a button to set the selection.
|
-3

You will need to select the entire value, and then manipulate the string in code. Depending on what you're trying to do with the words, you might look in to using regular expressions to match certain words/letters.

1 Comment

How does your code highlight a portion of text inside of a input textbox?

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.