I am trying to alert some message when every I select text (::selection) in a div. Can someone please give me some pointers
Also can I change the css of ::selection ? This doesn't seem to work.
$('::selection').css({.....
I am trying to alert some message when every I select text (::selection) in a div. Can someone please give me some pointers
Also can I change the css of ::selection ? This doesn't seem to work.
$('::selection').css({.....
Here's a JSFiddle example of doing just that with jQuery: http://jsfiddle.net/Lvx6bvth/1/
(function (window, document, $) {
var getSelectedText = function () {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
};
$(document.body).on("mouseup", function () {
var selectedText = getSelectedText();
if (selectedText.length !== 0) {
alert("You selected: " + selectedText);
}
});
})(window, document, jQuery);
Of course, you can delegate this to specific selectors so it only works on your targeted divs.
Function referenced from Get the Highlighted/Selected text