Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

disable text selection

Posted on 2011-02-24
8
Medium Priority
?
764 Views
Last Modified: 2012-05-11
I would like to disable text selection on an entire page except for one text box. I am able to disable text selection for the page, but have not had success in re-enabling it for the text box. I tried something like this:

var element = document.getElementById('txtKeyword');
 document.onselectstart = function () { return false; }
 element.onselectstart = function () { return true; }

...but that didn't work.

How can I enable text selection for only one element?

Thanks!
Amy

0
Comment
Question by:AmyL
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 34969861
0
 

Author Comment

by:AmyL
ID: 34970232
That shows how to disable for an element (which I can do currently). I can also disable text selection for the document.

What I would like to do is disable text selection for the entire document, then re-enable for one text box. The re-enabling is the part that is not working.

If there is another way to achieve this, I would be open to it. My main objective is to prevent my drag and drop from grabbing text along the way.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 34970245
i dont think that it is possible.

Since if you disable it for the document, it will propagate down to all the child elements.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:LFLFM
ID: 34970324
There's always the "transparent <div> over the text" trick.
If not, try these:
http://stackoverflow.com/questions/69430/is-there-a-way-to-make-text-unselectable-on-an-html-page

But remember, you can only fool the foolish; their computer HAS to HAVE the information to show it to the user, so its IMPOSSIBLE to prevent them from having the information in almost any format they want.
0
 

Author Comment

by:AmyL
ID: 34971319
I am not trying to fool anyone, I am trying to keep the drag and drop from looking messy.
0
 
LVL 3

Expert Comment

by:LFLFM
ID: 34972746
sorry, didn't mean to imply you were actually trying to fool anyone, my point was just that, if you were trying to protect your content from copy, that it was impossible.

:-)
0
 

Author Comment

by:AmyL
ID: 34975590
No problem :-) ...I was just clarifying that I actually don't care if they select the text on the page, I am just trying to fix it so that text won't be inadvertantly selected while doing the drag-and-drop.

I came up with a solution. I have an onload function:

var original_onselectstart;
window.onload = function()
{
_original_onselectstart = document.onselectstart;
 document.onselectstart = function () { return false; }
}

a toggling function:

function enableSelectStart(enable)
{
  if(enable)
  {
    document.onselectstart = original_selectstart;
  }
  else
{
    document.onselectstart = function() { return false; }
 }
}

and an event on my text box:

onFocus="enableSelectStart(true);" onBlur="enableSelectStart(false);"

This solution is working perfectly for me in IE...I'm not sure yet what to do with FireFox.
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 34977467
try this

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

check this
http://www.codingforums.com/archive/index.php/t-125810.html
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article discusses how to create an extensible mechanism for linked drop downs.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question