FF1+ IE5+ Opera7+

Disable Text Selection script

Author: Dynamic Drive

Note: Script rewritten Dec 28th, 06.

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:

Select All

With the script installed, just call the function disableSelection(target) at the very end of the document with a reference to the element you wish to disable text within. A few examples:
<script type="text/javascript">
disableSelection(document.body) //disable text selection on entire body of page
<script type="text/javascript">
var somediv=document.getElementById("mydiv")
disableSelection(somediv) //disable text selection within DIV with id="mydiv"
<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

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.

Limited time: 10% off ad! Use promo code "10pctoff"