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?
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?
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...
}
});