1

Say I have some text on a webpage.

The user then highlights a random selection of this text.

How can I know if the user's next click is on the highlighted/selected text?

Chris
  • 500
  • 6
  • 25

1 Answers1

0

Use the 'getSelection' method of the 'window' object to detect the click and get the text selected. Something like this:

document.addEventListener('click', function(event) 
{
    // Check if the user has selected any text
    var selection = window.getSelection();
    if (selection.toString().length > 0)
    {
        // Get the selected text
        var selectedText = selection.toString();
        // Do something with the selected text...
    }
});
Methark
  • 95
  • 8