Detect which word has been clicked or held down on within a text

once upon a time there was a great place for coding enthusiasts called StackOverflow

Consider the above content as part of a website content/article you just visited. it doesn’t matter which tag holds the data or content. for now, let’s take the

tag as an example.

If I use my mouse to hold on to the word for eg: time (I’m not selecting the word, just holding that specific word using my mouse pointer) and after releasing the mouse, I should be able to print that word to the console.

console.log(theWordIWasHoldingUsingMyMousePointer) // in this case output: time
Summary: Well, I was saying, if I visit a particular website and hold on to a particular word (can be any). In that case, after releasing the hold, I should be able to get that word to store into a variable or pass that word to an API.

Keep in mind that, the content can be inside any tag (h1, h2…, p, span, pre)

Is that possible, how can I do that in javascript?

You can use getSelection and some regex or string compare.

This Stack Overflow thread has some examples.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.