2

User is on a webpage , uses his mouse to select stuff, how can i use javascript to know what has been selected?

asdf
  • 21
  • 1
  • 2

2 Answers2

3

To get the raw text currently highlighted on the page you can do something like this:

function getSelectedText() {
    return window.getSelection ? window.getSelection() 
                               : document.selection.createRange().text;
}

Check an example of the above code here.

More info:

Christian C. Salvadó
  • 769,263
  • 179
  • 909
  • 832
1

If you're talking about the user dragging his mouse over text so that it's highlighted, use the Selection and Range objects (for Mozilla) and Selection and TextRange objects (for IE).

Tola Odejayi
  • 2,869
  • 9
  • 30
  • 46