Can I get selected text using jQuery?

I do not find anything. Is there a jQuery solution to extract selected text? I need to check the selected text for spaces, get the scroll style attributes and manage them based on this. I can do this part with regular expressions or whatever, but first I need access to the selected text! I'm struggling to find a one-time JavaScript solution, so if you have one of those that can help is very simple.

Thanks!

+6
source share
1 answer

You mean text selection with the mouse, so here, DEMO http://jsfiddle.net/yeyene/GYuBv/2/

$('#showSelected').on('click', function(){ var text = ""; if (window.getSelection) { text = window.getSelection().toString(); } else if (document.selection && document.selection.type != "Control") { text = document.selection.createRange().text; } alert(text); }); 

If you want to make some changes around the selected text, check out this DEMO http://jsfiddle.net/yeyene/GYuBv/3/

+16
source

Source: https://habr.com/ru/post/947801/


All Articles