2

I'm making an extension for Firefox (opens a highlighted word's definition on Wiktionary in a new tab). I got stuck with the text select part: the recommended window.getSelection().toString() doesn't work as window.getSelection() is undefined.

Things I tried:

JavaScript / jQuery: how to get selected text in Firefox (also undefined)

JavaScript / jQuery: get selection function not working in Firefox and Chrome (neither of the branches works)

window.getSelection() of textarea not working in firefox? (actually returns the string "undefined")

window.getSelection returning undefined or null (only for chrome)

Get the Highlighted/Selected text (either chrome only or undefined again)

Kotlopou
  • 415
  • 3
  • 13

1 Answers1

0

I ran into the same issue and after quite some digging I found something that works:

var textArea = document.getElementById('input_text_area');
var selectedText = textArea.value.substring(textArea.selectionStart,textArea.selectionEnd);

This other answer links to some background on why the above is necessary and why window.getSelection() doesn't work on Firefox, for example.