Given any block of text, using the following as an example:
Baseball is a sport.
A pitcher throws the baseball and the batter hits the baseball.
I need to determine, using JavaScript (use of jQuery is fine), which of the three instances of "baseball" as been selected by the user. I have code in place that will wrap the selected text in a span, which may help in figuring this problem out.
Assuming that the user selects the second occurrence of baseball, the HTML will look similar to this:
Baseball is a sport.
A pitcher throws the <span class="selection">baseball</span> and the batter hits the baseball.
Essentially I'm looking for a solution on how to determine that the selected 'baseball' is occurrence #2 (or 1 in the case of zero-indexing).
Any help is greatly appreciated.