NullVoxPopuli
NullVoxPopuli

Reputation: 65123

Get the html (source code) index of a selection?

If I have

<div>
hello
</div>

and I select 'hello' with my cursor, the start index is 6, end is 11 (inclusive).

Is there a way to get this information cross-browser with JavaScript or jQuery?

Upvotes: 3

Views: 414

Answers (1)

Manuel van Rijn
Manuel van Rijn

Reputation: 10305

If you've jQuery included you can do:

var selectionText = $(window.getSelection().getRangeAt(0).cloneContents()).text()

Upvotes: 1

Related Questions