1

I am trying to create a text button using simple span and formatting the text and providing onclick behaviour. The problem is when a user clicks on the button, it sometimes highlights the text of the button.

I want to avoid such behaviour, because it looks damn ugly when the text is selected. Is there any CSS/JavaScript/(jQuery) content I can use to avoid this?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Alec Smart
  • 90,128
  • 37
  • 118
  • 180
  • I see and smile when all mentions of javascript are suffixed by "jquery". Sometimes I feel that the community is getting out of touch with legacy javascript. – jrharshath May 28 '09 at 09:54
  • possible duplicate of [CSS rule to disable text selection highlighting](http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting) – Dan Jul 18 '13 at 08:57

3 Answers3

6
spanid.onselectstart = function() {return false;} // ie
spanid.onmousedown = function() {return false;} // mozilla

First result on Google by the way...

extra

$('#spanid').selectstart(function(event) {
  event.preventDefault();
});
Ropstah
  • 17,178
  • 23
  • 117
  • 187
5

For a CSS solution:

.unselectable {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none; /* Isn't Konquerour dead? */
    -moz-user-select: -moz-none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

But, looking here, CSS solution is not enough in late 2013, so you should add some javascript. There are good answers around.

Dan
  • 52,315
  • 38
  • 111
  • 148
tlrobinson
  • 2,682
  • 32
  • 35
1

You can just write:

$('#spanid').disableSelection();
Egoulet
  • 53
  • 2
  • 5