0

I want to change the color of selected text to #3C3 for the div one not div two.

<!DOCTYPE html>
<html>

    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="lib/jquery.mobile-1.3.1.min.css" />
        <script type="text/javascript" src="lib/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="lib/jquery.mobile-1.3.1.min.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        <script type="text/javascript" src="scripts/jquery.mobile-events.min.js"></script>
        <script type="text/javascript" src="scripts/jquery.scrollTo-1.4.2-min.js"></script>
        <link rel="stylesheet" href="css/test.css" />
        <script type="text/javascript" src="scripts/jquery.xpath.js"></script>
        <script type="text/javascript" src="scripts/test.js"></script>
    </head>

    <body>
        <div data-role="page" id="test-page" class="bg_main">
            <div data-role="header" id="bookmarkheader">.............. ...............</div>
            <div data-role="content" id="content" class="content_bg">
                <div class="one">some text</div>
                <div class="two">some text</div>
            </div>
            <div data-role="footer" id="footer_main">................. .................</div>
        </div>
    </body>

</html>

CSS

.one::selection {
       background-color:#3C3;
}

The above code didnt work. Can somebody please help me with this?

Omar
  • 32,302
  • 9
  • 69
  • 112
  • What browser are you using to test your functionality? Look [here](https://developer.mozilla.org/en-US/docs/Web/CSS/::selection) to check for the list of browsers that support this pseudo-element. By the way, for mozilla you should use `::-moz-selection` – Shalom Aleichem Jun 20 '13 at 05:47
  • I am using google chrome – user2454271 Jun 20 '13 at 05:50
  • It works but ONLY with background: http://jsfiddle.net/Gajotres/Y84W3/ – Gajotres Jun 20 '13 at 11:00

2 Answers2

0

If your mobile browser does not support ::selection pseudo-element, then what you can do is use JavaScript to trigger text selection change on the element and the replace the selected text in the div with a span with specified color.

Shalom Aleichem
  • 2,987
  • 2
  • 22
  • 34
0

Here is how, JQ way DEMO http://jsfiddle.net/yeyene/GYuBv/6/

JQUERY

$(function() {
    $('div.one').mouseup( function() {
        var mytext = selectHTML();
        $('.one span').css({"color":"red"});
    });
});

function selectHTML() {
    try {
        if (window.ActiveXObject) {
            var c = document.selection.createRange();
            return c.htmlText;
        }

        var nNd = document.createElement("span");
        var w = getSelection().getRangeAt(0);
        w.surroundContents(nNd);
        return nNd.innerHTML;
    } catch (e) {
        if (window.ActiveXObject) {
            return document.selection.createRange();
        } else {
            return getSelection();
        }
    }
}

HTML

    <div data-role="page" id="index">
        <div data-theme="b" data-role="header">
            <h1>Index page</h1>
        </div>

        <div data-role="content">
            <div class="one"><p>The quick brown fox jumps over the lazy dog</p></div>
            <div class="two"><p>The quick brown fox jumps over the lazy dog</p></div>                
        </div>
    </div>
yeyene
  • 7,297
  • 1
  • 21
  • 29