Highlight Selection Css

Change the background color of selected text with getSelection() method

by Chris

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
        <div id="div1" style="border: 1px solid #000;">
            <div id="test-text">
                <h1> Hello How are you </h1>
                <p >
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                </p>
            </div>
        </div>
        <br />
        
    </body>

CSS

.highlight
            {
                background-color: black;
                color: lime;
            }
            #test-text::-moz-selection { /* Code for Firefox */
               
                background: cyan;
            }

            #test-text::selection {
               
                background: brown;
            }

JavaScript

mouseXPosition = 0;
            $(document).ready(function () {
              
                $("#test-text").mousedown(function (e1) {
                    mouseXPosition = e1.pageX;//register the mouse down position
                });

                $("#test-text").mouseup(function (e2) {
                    var highlighted = false;
                    var selection = window.getSelection();
                    var selectedText = selection.toString();
                    var startPoint = window.getSelection().getRangeAt(0).startOffset;
                    var endPoint = window.getSelection().getRangeAt(0).endOffset;
                    var anchorTag = selection.anchorNode.parentNode;
                    var focusTag = selection.focusNode.parentNode;
                    if ((e2.pageX - mouseXPosition) < 0) {
                        focusTag = selection.anchorNode.parentNode;
                        anchorTag = selection.focusNode.parentNode;
                    }
                    if (selectedText.length === (endPoint - startPoint)) {
                        highlighted = true;

                        if (anchorTag.className !== "highlight") {
                            highlightSelection();
                        } else {
                            var afterText = selectedText + "<span class = 'highlight'>" + anchorTag.innerHTML.substr(endPoint) + "</span>";
                            anchorTag.innerHTML = anchorTag.innerHTML.substr(0, startPoint);
                            anchorTag.insertAdjacentHTML('afterend', afterText);
                        }

                    }else{
                        if(anchorTag.className !== "highlight" && focusTag.className !== "highlight"){
                            highlightSelection();  
                            highlighted = true;
                        }
                        
                    }


                    if (anchorTag.className === "highlight" && focusTag.className === 'highlight' &&...