Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected text in an input box

Is it possible to get the selected text in an input box of a website, using either jQuery or vanilla JavaScript?

I have tried with var selectedText = window.getSelection().toString();, but this code only gets the text in a paragraph and not in an input box.


EDIT: Maybe I was unclear, I want to get the text from a website that I didn't create. I'm building a Chrome extension and I need to get the text from an input box of a website.

like image 817
SarteLanda Avatar asked Sep 27 '15 14:09

SarteLanda


1 Answers

Came with solution Find below

function disp() {
  var text = document.getElementById("text");
  var t = text.value.substr(text.selectionStart, text.selectionEnd - text.selectionStart);
  alert(t);
}
<TEXTAREA id="text">Hello, How are You?</TEXTAREA><BR>
<INPUT type="button" onclick="disp()" value="show selected" />
like image 110
Manikanta Reddy Avatar answered Sep 26 '22 19:09

Manikanta Reddy