javascript - Storing Highlighted text in a variable -


Is there a javascript function that allows me to capture the text that is currently highlighted with the cursor And store it in a variable? I am selecting the document. Selection. Text but it does not work. Are there any possible alternatives? Here's the code:

  function more magic () {var output = document.selection.createRange (); Warning ("I work!");}  

When I run the function, it does not come in the statement of writing, so I know something is wrong.

Stealing from ungrassled

  function getSelectedText () {if (window .getSelection) {return window getSelection (); } And if (document.) {Return document. Selection. Range (). Text; } Return ''; }  

Use it in the "onclick" function or whatever else it will return the selected text in almost any browser.


Comments