Solved

selection highlight

Posted on 2014-04-23
1
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 57

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I verify that a jQuery getScript method has successfully loaded the script. 2 54
JQuery conflicts/syntax issue 4 30
Glitching Slide Show 19 37
JQuery & Classic ASP 10 36
DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
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…

739 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