2

I have an html file that displays some text. I need to call a javascript function when the user selects some text on it. I have the following code but the highlightSelection() function never gets called.

window.onselect = highlightSelection;

function highlightSelection() {

}

Is there any other way to call a JS function while user selects some text?

Dairo
  • 822
  • 1
  • 9
  • 22
Vaquita
  • 3,289
  • 5
  • 24
  • 43
  • 4
    There is no DOM event for text selection or 'highlighting'. [Here is a link with more information][1] [1]: http://stackoverflow.com/questions/3545018/selected-text-event-trigger-in-javascript – Jesse Harris May 28 '12 at 09:23
  • @JesseHarris Thanks for the quick replay – Vaquita May 28 '12 at 09:27

1 Answers1

2

you need to put the text in the appropriate element, an example:

<input type="text" value="This is some selectable text" onselect="showMsg()" />

function showMsg(){

}
Chenga
  • 36
  • 3