FF1+ IE5+ Opera7+ Disable Text Selection script

Note: Script rewritten Dec 28th, 06.



Description: This is a simple script that lets you disable text selection (the ability to drag and select text) within any element on your page, such as a certain paragraph. Script works in IE, Firefox, and Opera.

Demo: Try selecting text within the above paragraph- it's disabled.

Directions: Add this script to the HEAD section of your page:

<script type="text/javascript"> disableSelection(document.body) //disable text selection on entire body of page </script>

<script type="text/javascript"> var somediv=document.getElementById("mydiv") disableSelection(somediv) //disable text selection within DIV with id="mydiv" </script>

<script type="text/javascript"> var alltables=document.getElementsByTagName("table") for (var i=0; i<alltables.length; i++) disableSelection(alltables[i]) //disable text selection within all tables on the page </script>

With the script installed, just call the function disableSelection(target) at the veryof the document with a reference to the element you wish to disable text within. A few examples:

Make sure to call the above functions at the end of the document to ensure the element in which to disable text for is defined before the function is called.