Solved

disable text selection

Posted on 2011-02-24
8
753 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:gurvinder372
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:gurvinder372
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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:
gurvinder372 earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javascript delete path 7 45
Javascript question 8 44
Convert complicated date to yyyy-mm-dd format 22 53
How can I use javascript variable in mysql query 21 46
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 demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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