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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jay
  • 3,517
  • 5
  • 26
  • 44
  • 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

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