views:

53

answers:

3

How could I get the selected text in the following code ?

I working with Firefox 3.6.3 (currently not interested in other browsers).

HTML:

<input id="my_text_field" type="text" />
<div id="log"></div>

JavaScript:

$("#my_text_field").select(function() {
   var selected_text = "Something selected"; // What should be here ?
   $("#log").append(selected_text + "<br />");
});
+2  A: 

Check out this other SO post. Hope this helps. ;)

http://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-javascript

Lance May
A: 
var u_sel;
if(window.getSelection){
  u_sel = window.getSelection();
   // u_sel.text()   InternetExplorer !!
  alert(u_sel);
}
jAndy
+1  A: 

Have a look at this plugin:

http://labs.0xab.cd/jquery/fieldselection/0.2.3-test/test.html

Scott Christopherson