0

When I am selecting bunch of rows by pressing shift key in gridview, the default text selection(dark blue) mixed with my custom row selection color.

How to disable the default text selection in web application.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Hukam
  • 1,016
  • 18
  • 40

3 Answers3

2

This works for me to disable "selection" in both FF and IE:

// jQuery Solution
$(document).mousedown(function (e)
{
   return false;
});

$(document).bind("selectstart", function (e)
{
   return false;
});

If you're not using jQuery, here's the plain javascript solution.

// Vanilla Javascript Solution
function attachEvent(element, eventName, handler)
{
    if(element.addEventListener)
    {
        element.addEventListener(eventName, handler, false);
    }
    else
    {
        element.attachEvent("on" + eventName, handler);
    }
}

attachEvent(document, "mousedown", function (e)
{
  if(window.addEventListener)
  {
     e.preventDefault();
  }
  return false;
});

attachEvent(document, "selectstart", function (e)
{
  return false;
});
TheCloudlessSky
  • 18,608
  • 15
  • 75
  • 116
  • 1
    @chand - Just remember that this is disabling "mousedown" for the *whole* document object. You might want to give it a little bit of scope... – TheCloudlessSky Jul 29 '10 at 14:41
  • 1
    @chand - For FF specifically you can also do `document.body.style.MozUserSelect = "none";` to disable selection. – TheCloudlessSky Jul 29 '10 at 14:45
0

Browser select happens when you click somewhere and then shift+mousedown somewhere else, so you need to cancel the second mousedown. To do this, you need to add

return false;

to your onmousedown event handler.

Andy E
  • 338,112
  • 86
  • 474
  • 445
0

You mean your text gets selected?

You have to disabled text selection during your selection, see here how to do it.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452