Solved

selection highlight

Posted on 2014-04-23
1
285 Views
Last Modified: 2014-04-23
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.
0
Comment
Question by:tonelm54
1 Comment
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40017063
Have you had a look at the examples on this site

http://help.dottoro.com/ljtfkhio.php

They show how to select text - Example 3 shows how to get the seleted text.

Example 3 Repeated below
<head>
    <script type="text/javascript">
        function GetSelection () {
            var selection = "";

            var textarea = document.getElementById("myArea");
            if ('selectionStart' in textarea) {
                    // check whether some text is selected in the textarea
                if (textarea.selectionStart != textarea.selectionEnd) {
                    selection = textarea.value.substring  (textarea.selectionStart, textarea.selectionEnd);
                }
            }
            else {  // Internet Explorer before version 9
                    // create a range from the current selection
                var textRange = document.selection.createRange ();
                    // check whether the selection is within the textarea
                var rangeParent = textRange.parentElement ();
                if (rangeParent === textarea) {
                    selection = textRange.text;

                }
            }

            if (selection == "") {
                alert ("No text is selected.");
            }
            else {
                alert ("The current selection is: " + selection);
            }
        }
    </script>
</head>
<body>
    <textarea id="myArea" spellcheck="false">Select some text within this field.</textarea>
    <button onclick="GetSelection ()">Get the current selection</button>
</body>

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question