tags:

views:

589

answers:

2

Hi,

I would like to add text to my webpage as a label and make it unselectable.

In other words, When the mouse cursor is over the text I would like it to not turn into a text selecting cursor at all.

A good example of what i'm trying to achieve is the buttons on this website (Questions,Tags,Users,...)

Thanks!

+5  A: 

You can't do this with plain vanilla HTML, so JSF can't do much for you here as well.

Your best bet is using JavaScript for this. Here's an SSCCE, copy'n'paste'n'run it.

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2310734</title>
        <script>
            window.onload = function() {
                var labels = document.getElementsByTagName('label');
                for (var i = 0; i < labels.length; i++) {
                    disableSelection(labels[i]);
                }
            };
            function disableSelection(element) {
                if (typeof element.onselectstart != 'undefined') {
                    element.onselectstart = function() { return false; };
                } else if (typeof element.style.MozUserSelect != 'undefined') {
                    element.style.MozUserSelect = 'none';
                } else {
                    element.onmousedown = function() { return false; };
                }
            }
        </script>
    </head>
    <body>
        <label>Try to select this</label>
    </body>
</html>

If you're already using jQuery, then here's another example which adds a new function disableSelection() to jQuery so that you can use it anywhere in your jQuery code:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2310734 with jQuery</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
        <script>
            $.fn.extend({ 
                disableSelection: function() { 
                    this.each(function() { 
                        if (typeof this.onselectstart != 'undefined') {
                            this.onselectstart = function() { return false; };
                        } else if (typeof this.style.MozUserSelect != 'undefined') {
                            this.style.MozUserSelect = 'none';
                        } else {
                            this.onmousedown = function() { return false; };
                        }
                    }); 
                } 
            });

            $(document).ready(function() {
                $('label').disableSelection();            
            });
        </script>
    </head>
    <body>
        <label>Try to select this</label>
    </body>
</html>
BalusC
+1 - just what i needed. thanks.
Sky Sanders
A: 

do it in css with a simple:

body {cursor:default;}
stillstanding
I think you misunderstood the question.
BalusC
the question wasn't clear. a "non-text selecting cursor" could mean the default pointer or a hand pointer. in both cases, the CSS could be either .anything {cursor:default;} or .anything {cursor:pointer;} which solves the problem.
stillstanding
Hi Rockjock, Thanks for the answer.The part about the cursor was only a part of the question. The title of the question is pretty clear "How to make HTML Text unselectable."Whatever the cursor looks like, styling doesn't prevent me from selecting it.
Ben