Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

selection highlight

Is it possible to highlight text in jQuery via code?

Im trying to do my own autocomplete, and want to 'autocomplete' the text box with some text, so if the user presses a key then the highlighted text disappears.

What Im trying to do is set the highlight range, such as:-
    $('#txtTest').keyup(function () {
        var currLen = $('#txtTest').val();
            
        $('#txtTest').val($('#txtTest').val() + " Hello");
        $('#txtTest').attr({
            selectionStart: currLen,
            selectionEnd: 999
        });
    });

Open in new window

So if you put in Bob in the textbox, then it will return 'Bob Hello' with ' Hello' highlighted.

Also I need to know what text is not highlighted, such as:-
    $('#txtTest').keyup(function () {

        var currUnselectedText = $('#txtTest').val().substring(1,$('#txtTest').selectionStart());
        });
    });

Open in new window


My issue is I cannot find out any way of setting or getting highlighted text.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial