I'm looking for a way to check if some text is selected (or not) in a specific element.
So far all I can find is a way to get selected text in the whole document with window.getSelection
. I'd like to check if some text is selected in #myDiv
only.
If text is selected in #myDiv
, the function should return true. If text is selected elsewhere, or no text is selected, the function should return false.
Something like:
$.fn.isTextSelected = function(method) {
return this[0].selectionEnd - this[0].selectionStart > 0 ? true : false;
}
$('#myDiv').isTextSelected();
Can I do this with a simple jQuery method?