Bandit Design » Our Design Our Portfolio Contact Us
TumbleLog Our Thoughts
< Back
Javascript Disable Text Selection
Tutorial by James of Bandit.co.nz
Copyright James Nisbet 2005 ~ 2007

20th December 2005


#####
NOTE: I'VE RELEASED AN UPDATE TO THIS SCRIPT HERE: http://lab.bandit.co.nz/scripts/disableselect/
#####

In the following tutorial I will explain how to disable the selection of text on a page.

It's very simple - you use two of the built in JavaScript events to cancel the selection before it starts. The following code disables selections globally over the entire document.



window.onload = function() {
document.onselectstart = function () { return false; } // ie
document.onmousedown = function () { return false; } // mozilla
}


Note that you can attach the events to any element - in the following example I'll disable selecting text in an element with the id 'content'.



window.onload = function() {
var element = document.getElementById('content');
element.onselectstart = function () { return false; } // ie
element.onmousedown = function () { return false; } // mozilla
}


Note that I'm calling the code after the page has loaded - this makes sure that the element is fully loaded before trying to do anything with it.

Have fun. If you have any issues, please drop me a line.

If you liked this tutorial please tell a friend.


< Back

Page dynamically created in 0.002 seconds by morBandit's phpTutorial v28.02.05. 12229 page views.