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?

2
  • 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. Commented May 28, 2009 at 9:54
  • possible duplicate of CSS rule to disable text selection highlighting Commented Jul 18, 2013 at 8:57

3 Answers 3

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for such a simple solution :) I didn't realize it would be this simple
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.

Comments

1

You can just write:

$('#spanid').disableSelection();

1 Comment

just to clarify, this requires jquery library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.