Reputation: 31
I'm writing a browser add-on and want to get the word / words the user is currently hovering over via Javascript.
Upvotes: 3
Views: 1184
Reputation: 8043
My guess is that you cannot get this done unless every word is in its own container like etc and you have declared a onmouseover handler for this. An easier approach would be use the currentSelection element when a user double clicks or mouse selects a piece of text on the browser. You could write on mouse click/doubleclick events for this.
Upvotes: 0
Reputation: 324547
One option is to place every word inside its own <span>
element and adding a mouseover
handler to the body that checks the event's target
/ srcElement
property to retrieve the span and therefore the word. This has significant downsides: the initial process of surrounding each word with a span could be slow; the new spans could mess up existing CSS rules; the document would end up with loads of elements that have no semantic value.
Upvotes: 1
Reputation: 38603
There's no good general purpose function to do that. If you specify which browser you're adding on to, there might be a specific workaround. When faced with this problem in the past, I had to resort to asking the user to double-click the word (then you can detect the double-click, get selection, and reset it back).
Upvotes: 1