Hello Reader's!
If you want to show what the text user has selected in a alert box, Then by using Javascript you can perform this task very quickly.
Step1:-
<div>Some test text for you to select</div>
Make a div that shows the text.
Now write the JS as follows:-
function getSelectedText() {
var text = "";
if (typeof window.getSelection != "undefined") {
text = window.getSelection().toString();
} else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
text = document.selection.createRange().text;
}
return text;
}
function doSomethingWithSelectedText() {
var selectedText = getSelectedText();
if (selectedText) {
alert("Got selected text " + selectedText);
}
}
document.onmouseup = doSomethingWithSelectedText;
document.onkeyup = doSomethingWithSelectedText;
Now what ever user will select the text, it will be shown in alert box
0 Comment(s)