Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

selection highlight

Posted on 2014-04-23
1
Medium Priority
?
291 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 59

Accepted Solution

by:
Julian Hansen earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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…

688 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