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,738
  • 21
  • 105
  • 131
Alec Smart
  • 94,115
  • 39
  • 120
  • 184
  • 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,538
  • 24
  • 120
  • 194
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
  • 55,715
  • 40
  • 116
  • 154
tlrobinson
  • 2,812
  • 1
  • 33
  • 35
1

You can just write:

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