Rana
Rana

Reputation: 4611

Save highlighted text position in HTML using javascript

I'm able to select the text using window.getSelection() How can I get the character position of the start and end of the selection in the HTML code? So this information can be saved to the server.

Upvotes: 2

Views: 3152

Answers (3)

Tim Down
Tim Down

Reputation: 324627

You can't reliably get the offset of the user selection in terms of the original HTML source of the document. You can only get it in terms of nodes and offsets within nodes. I'd recommend looking at the following answer to a similar question: Calculating text selection offsets in nest elements in Javascript

Upvotes: 0

Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13827

I feel there is no built-in functions for getting start and end position of selection code. you need to write some JS coding for getting these position. I've wrote just simple coding. I'm not sure whether it will be useful or not. But, check it out.

<html>
    <head>
        <script>
            function GetSelectedText () 
            {
                var fullString = "I feel there is no built-in functions for getting start and end position of selection code. you need to write some JS coding for getting these position. I've wrote just simple coding. I'm not sure whether it will be useful or not. But, check it out.";
                if (window.getSelection) 
                    {               
                        var range = window.getSelection ();                     
                        var startPosition = fullString.search(range);
                        var getPosition = range.toString();
                        var endPosition = parseInt(getPosition.length) + parseInt(startPosition)
                        alert ("Start position : " + startPosition + " and End position : " + endPosition);        
                    }
                else
                    {
                        if (document.selection.createRange)
                        {
                            var range = document.selection.createRange ();
                            var startPosition = fullString.search(range.text);
                            var getPosition = range.text;
                            var endPosition = parseInt(getPosition.length) + parseInt(startPosition);
                            alert ("Start position : " + startPosition + " and End position : " + endPosition);
                        }
                    }       
            }
        </script> 
    </head>
    <body>
        <button onclick="GetSelectedText ()">
            Get 
        </button>
        I feel there is no built-in functions for getting start and end position of selection code. you need to write some JS coding for getting these position. I've wrote just simple coding. I'm not sure whether it will be useful or not. But, check it out.
    </body>
</html>

For this coding, you need to define your text message in two places. One is to in JS coding

var fullString = "??"

and another is to Body message below of Button/

Upvotes: 3

Ankit Jaiswal
Ankit Jaiswal

Reputation: 23427

I think the shorter way would be to use string.indexOf() function of javascript. It simply returns the starting index of the searched string in the larger string and -1 if it is not present. The end index can be calculated by adding the length of the substring with starting index.

Upvotes: 0

Related Questions