Solved

selection highlight

Posted on 2014-04-23
1
282 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 51

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now