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.
3 Answers
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/

- 13,309
- 42
- 142
- 227
-
i got 0 for both start and end position – Rana Aug 09 '10 at 20:30
-
Sorry. I've fixed some bugs and edited above coding. Dun forget to follow my instruction. – PPShein Aug 10 '10 at 02:12
-
3The problem with this code is that if you select something with more than one occurrence in the text, it will return the position of the first one. i.e., in the above code, selecting "for" will always return startPosition = 38 no matter which "for" you choose. – Ken Bellows Dec 03 '10 at 19:44
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.

- 22,859
- 5
- 41
- 64
-
what about duplicate text? if the user selects "the", the first occurrence of indexOf might not be the selected word? – Rana Aug 09 '10 at 20:30
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