Content with Style

Web Technique

Disabling text selection

by Mike Stenhouse on May 11 2006, 13:03

While trying to make a table behave in an Excel fashion I came across a little problem. I wanted the table to allow the user to select a range of rows with a Shift+Click… The problem is that, by default, the browser will select the page’s text content. This isn’t something you’ll want to do very often but it is possible to get around this:

Firefox can do this from your CSS:


table {
    -moz-user-select: none;
}

On the table element IE needs:


the_table_node.onselectstart = function () {
    return false;
};

Comments

  • Holy cow! I\'ve been working on this for a whole day trying to figure out how to get the darn Shift key to not select background page text. I\'m creating a custom ListBox control in .NET implemented with a table to provide custom highlighting. Plugged in your code above and it works like magic. Thank you SO MUCH!! :)

    by Jeromie Walters on April 22 2009, 16:02 #