Home > Web Development > Disable Text Selection using JavaScript

Disable Text Selection using JavaScript

I’m currently developing a CMS (Content Management System) at work and needed a way to disable text selection on certain elements. I was aware of the Mozilla proprietary css property ‘-moz-user-select’ which takes the value ‘none’ to disable text but this is hardly a cross-browser solution. I did a quick Google, and found the following JavaScript function at Ajax Cookbook which attempts to disable text selection for all modern browsers:

function disableSelection(element) {
element.onselectstart = function() {
return false;
};
element.unselectable = "on";
element.style.MozUserSelect = "none";
element.style.cursor = "default";
}

View Full Article at Ajax Cookbook

Bookmark and Share

Cameron Web Development ,


  1. No comments yet.
  1. No trackbacks yet.