0

I'm trying to write JavaScript that manages the text a user selects in a webpage, but I'm not sure where to begin; i.e., I was wondering if there was a way to limit the ability to select/highlight text so that the selection ends at a terminating punctuation mark, and the user cannot select anymore (And possibly trigger an alert window or send an event when that limit is first reached). Also, is there a way to change the colour of highlighted/selected text in a browser? I'm familiar with JavaScript pointer and click events, but I'm having trouble finding any information on what I'm looking for.

Thanks in advance.

Joshua Jennings
  • 517
  • 1
  • 4
  • 13

1 Answers1

2

You want to look at window.getSelection().getRangeAt(0);

I found this answer pretty useful when I was looking at that:

https://stackoverflow.com/a/12823606

The context there is specifically highlighting, but obviously once you have the text you can do other things with it.

Community
  • 1
  • 1
David Carlisle
  • 5,582
  • 1
  • 19
  • 23