user2067005
user2067005

Reputation: 839

Jquery selected value of highlighted text

Im trying to make my editor where when you highlight text on your computer, jquery will take that selected value and throw the tags around it, more specificity code or pre tags.

    var Selectedvalue =  // set highlighted selection value
        $("#content").contents().find("body").append($("<pre></pre>").append(Selectedvalue))
   ;

I already know how to get the value between the tags, i just need to know how to get the value.

Upvotes: 2

Views: 254

Answers (2)

Dipesh Parmar
Dipesh Parmar

Reputation: 27354

Well from googling of few time and found out http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html i am sure this is it.

Example

Response to OP comment

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">

    $(function()
    {
        var ifx = $('<iframe src="code/sample.html" height=200 width=200></iframe>').appendTo(document.body);

        $(document.body).bind('mouseover', function()
        {
            var u_sel;
            if(window.getSelection)
            {
                u_sel = ifx[0].contentWindow.getSelection();
                alert(u_sel);
            }
        });
    });

</script>

Upvotes: 1

Hary
Hary

Reputation: 5818

try something like this

function getSelectionText() {
var text = "";
if (window.getSelection) {
    text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
    text = document.selection.createRange().text;
}
return text;

}

Upvotes: 1

Related Questions