Highlight Selection

HTML

<p class="pattern"> amrit suri
        elit, varius eu hendrerit tempor, cursus id enim. Curabitur viverra, quam sed
        convallis aliquet, quam quam <span class="highlighted">aliquam </span>nisl, nec aliquam metus sapien vel mi.
        Nulla facilisi. Cum sociis natoque penatibus et magnis dis parturient montes,
        nascetur ridiculus mus. Nam rutrum, magna id tincidunt commodo, nisi libero
        mollis lectus, a congue dui arcu at dolor. Vivamus at mollis nisi. Nunc sagittis
        ornare pretium. Fusce euismod ornare nisi eu facilisis. Ut in quam in lectus
        sollicitudin commodo vel sed enim. Maecenas pulvinar dolor at urna dapibus
        pulvinar. Pellentesque tempor massa eget tellus consequat at dignissim tortor
        rhoncus. Ut eu leo risus, ut auctor mi. Integer interdum imperdiet blandit.
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent imperdiet
        mollis justo id gravida. Phasellus condimentum diam quis lorem posuere pretium.
        </p>

CSS

p.pattern span.highlighted   {background: red;}

JavaScript

//Page Load

// Capture the highlighted text on a page.
//$(document).mouseup(function(){
//    var text = (document.all) ? document.selection.createRange().text : document.getSelection();
//    
//    // Find the text on the page.
//    var paragraph = $('p.pattern').html();
//    
//    // Add a class to the text.
//    $('p.pattern').html(paragraph.replace(text, '<span class="highlighted">' + text + '</span>'));
//});

            /**
             * Copyright (c) 2009 Mark S. Kolich
             * http://mark.kolich.com
             *
             * Permission is hereby granted, free of charge, to any person
             * obtaining a copy of this software and associated documentation
             * files (the "Software"), to deal in the Software without
             * restriction, including without limitation the rights to use,
             * copy, modify, merge, publish, distribute, sublicense, and/or sell
             * copies of the Software, and to permit persons to whom the
             * Software is furnished to do so, subject to the following
             * conditions:
             *
             * The above copyright notice and this permission notice shall be
             * included in all copies or substantial portions of the Software.
             *
             * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
             * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
             * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
             * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
             * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
             * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
             * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
             * OTHER DEALINGS IN THE SOFTWARE.
            */
            
            if(!window.Kolich){
                Kolich = {};
            }
            
           ...