Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get highlighted text with JQuery? [duplicate]

I'm not finding anything anywhere. Is there a JQuery solution to retrieve highlighted text? I need to check the highlighted text for spans, get those spans' style attributes, and manipulate them based on that. I can do that part with regular expressions or whatever obviously, but first I need access to the highlighted text! I'm struggling to even find a cross-browser plain javascript solution, so if you've got one of those handy that would help immensely as well.

Thanks!

like image 670
Mr. Lavalamp Avatar asked Jun 21 '13 03:06

Mr. Lavalamp


1 Answers

You mean text selection with mouse, so check 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 do some changes around selected text, check this DEMO http://jsfiddle.net/yeyene/GYuBv/3/

like image 115
yeyene Avatar answered Oct 04 '22 18:10

yeyene