Reputation: 21
In editor i need to get the word using right click. But that word doesn't selected.
var word=ed.selection.getContent();
this code only working for selected text.
Or how can i get the word from textarea using right click in javascript?
Upvotes: 1
Views: 789
Reputation: 2533
I'm not sure what exactly do you expect of this feature and I don't know why don't you use doubleclick to select word with default behavior?
However it is possible. Here is an example. But first you have to click on text (or word that you need to select with mouse-1) and then click mouse-2:
document.querySelector('textarea').addEventListener('contextmenu', function (e) {
e.preventDefault();
var startPosition = this.selectionStart,
endPosition = this.selectionEnd;
while (this.value.charAt(startPosition) !== ' ' && startPosition >= 0) {
startPosition--;
}
while (this.value.charAt(endPosition) !== ' ' && endPosition < this.value.length) {
endPosition++;
}
this.selectionStart = startPosition + 1;
this.selectionEnd = endPosition;
})
<textarea>This is some text. Click on any word and then do right click</textarea>
Upvotes: 3