jay
jay

Reputation: 3667

Execute a JavaScript function when user selects some text in an html file

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?

Upvotes: 2

Views: 267

Answers (1)

Chenga
Chenga

Reputation: 36

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(){

}

Upvotes: 2

Related Questions