views:

106

answers:

3

Hi every body!

I am making a html/css/jquery gallery, with serveral pages.

I indeed have a "next" button, which is a simple link with a jquery click listener.

The problem is that if the user click the button serveral times, the text of the button is selected, and then the full line of text. In my really darky design, that is really ugly and nonsensical.

So here is my question: can you disable text selection on html? If not, i'll terribly miss flash and its high level of configuration on textfields... grrr :(

Thank you for your help! David

A: 

I'm not sure if you can turn it off, but you can change the colors of it :)

myDiv::selection,
myDiv::-moz-selection,
myDiv::-webkit-selection {
    background:#000;
    color:#fff;
}

Then just match the colors to your "darky" design and see what happens :)

Kyle Sevenoaks
You could compress this into one CSS rule. myDiv.webkit::-webkit-selection, myDiv.moz::-moz-selection, myDiv.normal::selection{ background:#000; color:#fff;}
yc
that doens't work by me:
daviddarx
@yc: use a multiple selector, I shall edit, thanks :)
Kyle Sevenoaks
#galleryPagesNavigation a.normal::selection { background:#000; } #galleryPagesNavigation a.moz::-moz-selection { background:#000; } #galleryPagesNavigation a.webkit::-webkit-selection { background:#000; }
daviddarx
but thank you for your quick answer! :)
daviddarx
You have "normal" "moz" and "webkit" in there, remove those, copy the updated code out of this answer :)
Kyle Sevenoaks
that still doesn't work, sorry
daviddarx
+3  A: 
<div style='-moz-user-select: none;-webkit-user-select: none;' onselectstart='return false;'>Blabla</div>

Works in Safari, Firefox and Internet Explorer. Should work also for Chrome but I can't check.

Jerome
yeah! That one work! thank you
daviddarx
Mark as answer then.
Jeaffrey Gilbert
Still don't work in safari and chrome. I keep that solution, but i also implement one work around for the rest: at each click, replace the html of the link by the html of the link. the text is then updated, and the selection go off after 1 half second!
daviddarx
The CSS for webkit is similar to the one for Firefox, I edited the answer to add it.
Jerome
A: 

You can use JavaScript to do what you want:


if (document.addEventListener !== "undefined") {
  // Not IE
  document.addEventListener('click', checkSelection, false);
} else {
  // IE
  document.attachEvent('onclick', checkSelection);
}

function checkSelection() {
    var sel = {};
    if (window.getSelection) {
        // Mozilla
        sel = window.getSelection();
    } else if (document.selection) {
        // IE
        sel = document.selection.createRange();
    }

    // Mozilla
    if (sel.rangeCount) {
        sel.removeAllRanges();
        return;
    }

    // IE
    if (sel.text > '') {
        document.selection.empty();
        return;
    }
}

Soap box: You really shouldn't be screwing with the client's user agent in this manner. If the client wants to select things on the document, then they should be able to select things on the document. It doesn't matter if you don't like the highlight color, because you aren't the one viewing the document.

jsumners