Solved

Disable onMouseDown

Posted on 2004-09-27
31
755 Views
Last Modified: 2008-02-20
Hello experts

How can I disable all mouse clicks in a textarea?

I have tried the following with no avail:

<textarea onMousedown="return false;" onClick="return false;">some text.</textarea>

Please help.


0
Comment
Question by:hans_larson
[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
  • 14
  • 9
  • 6
  • +2
31 Comments
 
LVL 33

Expert Comment

by:sajuks
ID: 12166337
// try this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script language="javascript">
function nomdwn()
     {

     if (event.button==2 && event.srcElement.tagName =='TEXTAREA')
          {
          alert('No Clicks Allowed');
          }
     }
document.onmousedown=nomdwn
</script>
</HEAD>

<BODY>
<TEXTAREA>some text.</TEXTAREA>

</BODY>
</HTML>
0
 
LVL 5

Expert Comment

by:darksinclair
ID: 12166339
you could try the following...

<textarea onmousedown="this.blur();" onClick="this.blur();">Some text.</textarea>

That will take the focus off the textarea as soon as they click on it.

Cheers
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166391
sajuks: it didn't work. I don't want any messages to pop - It should just discard the click or mousedown.

 darksinclair: I cannot have the textarea loose focus.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Expert Comment

by:viola123
ID: 12166399
<HTML>
<HEAD>
<script language="javascript">
function disableMouse()
{
      if ((event.button==2 || event.button==1) && event.srcElement.tagName =='TEXTAREA')
            alert('No Clicks Allowed');
}
document.onmousedown=disableMouse
</script>
</HEAD>
<BODY>
<TEXTAREA ID="Textarea1" NAME="Textarea1"></TEXTAREA>
</BODY>
</HTML>
0
 
LVL 6

Expert Comment

by:viola123
ID: 12166413
hi, Hans:

if you don't want any message, this code should be perfect to you

<HTML>
<HEAD>
<script language="javascript">
function disableMouse()
{
      if ((event.button==2 || event.button==1) && event.srcElement.tagName =='TEXTAREA')
            return;
}
document.onmousedown=disableMouse
</script>
</HEAD>
<BODY>
<TEXTAREA ID="Textarea1" NAME="Textarea1"></TEXTAREA>
</BODY>
</HTML>


cheers
viola
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12166444
In IE unfortunately its not possible to disable withought a alert.
In case you dont want the mouse click in the whole page
<script language=JavaScript>
<!--
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all)) {

if ( (e.which==2||e.which==3) && event.srcElement.tagName =='TEXTAREA')
{
    (message);
    return false;
}

}
}

if (document.layers)
    {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else    {document.onmouseup=clickNS;document.oncontextmenu=clickIE;}// -->
</script>
<body>
<TEXTAREA>some text.</TEXTAREA>
</body>
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166489
viola123: the code without the alert does not work.
 sajuks: the code doesn't work.

If I truly cannot disable the mouse clicks in the textarea, does someone know if you can place the cursor in a specific point in the text area? Let's say Line 5, character 12.

0
 
LVL 33

Expert Comment

by:sajuks
ID: 12166501
forgot to change the body tag, it should look like this
<body onContextMenu="return false">

<script language="javascript">
 var browser=window.navigator.appName;

 function onMouseDownHandler(e) {
    if (e.which==3) {
       return false;
    }
 }

 if (browser == "Netscape") {
    if (document.layers) {
       window.captureEvents(Event.MOUSEDOWN);
       window.onmousedown=onMouseDownHandler;
    }
 }
</script>



<body onContextMenu="return false">


<TEXTAREA>some text.</TEXTAREA>

</BODY>
</HTML>
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166513
sajuks: I need to disable the onMouseDown and onClick events, not the context menu.
0
 
LVL 6

Expert Comment

by:viola123
ID: 12166528
hi,Sajuks:

it seems the mouse is disable not only over the textarea but also the whole page.

hehehe.... plz, make it less powerful...^_^

viola
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166538
What browsers are you guys using? I'm running IE6 and it doesn't even disable the mouse on the textarea.

Thanks for your help.
0
 
LVL 6

Expert Comment

by:viola123
ID: 12166553
i am using ie6

viola
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166564
Are we talking about the same thing? I need to have both mouse buttons disabled.

Howcome it works for you and not for me?
0
 
LVL 6

Expert Comment

by:viola123
ID: 12166584
i combined the code from Sajuks and mine.

try it, if it is not working this time, we have to talk about browser issue.

<HTML>
<HEAD><title></title>
<script language=JavaScript>
<!--
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all)) {

if ( (e.which==2||e.which==3) && event.srcElement.tagName =='TEXTAREA')
{
    (message);
    return false;
}

}
}

if (document.layers)
    {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else    {document.onmouseup=clickNS;document.oncontextmenu=clickIE;}// -->
</script>
</head>
<body ONSELECTSTART = "event.returnValue=false;">
<TEXTAREA ID="Textarea1" NAME="Textarea1">some text.</TEXTAREA>
</body>
</HTML>

cheers
viola
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166614
viola:

It's not working. I see that you're only capturing mouse button 2 and 3. I also need button 1(left mouse button). I tried adding and it still didn't work.

Here's what happens. I have the following sentence:

some text.<--- Cursor is blinking after the period.

Now, I click with the mouse cursor on the "t" in the same line then the cursor starts blinking by the "t".-- I don't want that to happen. This is what I need to disable.

Thanks.
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166630
I know you can disable key presses with the following:

        event.cancelBubble = true;
        event.returnValue = false;
        return false;

That doesn't seem to work for the mouse buttons. Any ideas?
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12166634
@viola123, my comment was marked with the lines that it would disable for the whole page. :-)
As does the  code which you rewrote try with an input text <INPUT TYPE="text" NAME="">
   



0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166646
It's okay if it disables the mouse for the whole page.

I need it to disable the mouse for the textarea too. There aren't <INPUT TYPE="text" NAME=""> objects on this page.
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12166667
// this previously posted  code doesnt work for you ??
<script language="javascript">
 var browser=window.navigator.appName;
 function onMouseDownHandler(e) {
    if (e.which==3) {
       return false;
    }
 }
 if (browser == "Netscape") {
    if (document.layers) {
       window.captureEvents(Event.MOUSEDOWN);
       window.onmousedown=onMouseDownHandler;
    }
 }
</script>
<body onContextMenu="return false">
<TEXTAREA>some text.</TEXTAREA>
</BODY>
</HTML>
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166677
No, it does not work. I'm running IE 6.0.3790.0
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12166691
Works in both IE6 sp1, Mozilla/5.0.
Is this the only code in your page or are you trying to merge this with an existing code ?
Have you cleared all your temp files/history and tried again ?
0
 
LVL 6

Expert Comment

by:viola123
ID: 12166694
i am using exactly the same IE as yours. weird~~~~~~~~~~~~~~~~~~~~~~

viola
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166697
I tried the following code that you posted and it works with Mozilla Firefox 0.9.3.

I need it to work in IE though.   :)

-------------------------------------------------------------------------------------

<script language="javascript">
 var browser=window.navigator.appName;
 function onMouseDownHandler(e) {
    if (e.which==3) {
       return false;
    }
 }
 if (browser == "Netscape") {
    if (document.layers) {
       window.captureEvents(Event.MOUSEDOWN);
       window.onmousedown=onMouseDownHandler;
    }
 }
</script>
<body onContextMenu="return false" onmousedown="return false;">
<TEXTAREA>some text.</TEXTAREA>
</BODY>
</HTML>


0
 
LVL 33

Expert Comment

by:sajuks
ID: 12166737
Can you try this from some other machine ?Maybe a newer ie version. Though this should've worked in IE also.
(Does in mine )
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166749
I just tried it on another PC with IE 6.0.2800.1106.xpsp2.030422-1633 and I get the same problem.

I also tried clearing the cache and I copied just the sample code to a new page, no other code involved and gave it a new file name and still the same thing.
0
 
LVL 33

Accepted Solution

by:
sajuks earned 50 total points
ID: 12166762
Well my OS is w2k, whats yours ?
heres a few more varns, see if any of these work
1
<script>
if (document.layers) {
   window.captureEvents(Event.MOUSEDOWN);
   document.onmousedown=right;
   window.onmousedown=right;
}

function right(e) {
   if (e.which == 3 || e.which == 2) return false;
   return true;
}
</script>
<body onContextMenu="return false">
----------------------------------------------------------------------------------------
2

<script language="JavaScript1.1">
<!--
var ns4 = (document.layers)? true:false
var ie4 = (document.all)? true:false
function press(e) {
  if ((ns4 && (e.which == 3 || e.which == 2)) || (ie4 && (event.button == 2 || event.button == 3))) return false
  return true
}
for (var i=0; i<document.images.length; i++) document.images[i].onmousedown=press
for (var i=0; i<document.links.length; i++) document.links[i].onmousedown=press
document.onmousedown=press
if (ns4) window.captureEvents(Event.MOUSEDOWN)
window.onmousedown=press
//-->
</script>
</head>
<body oncontextmenu="return false">
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166769
I'm running Windows 2003 server on the one pc and Windows XP Pro. on the other.

I tried the two examples above and still the same thing.

0
 
LVL 33

Expert Comment

by:sajuks
ID: 12166794
Thats odd. Dont know if am missing something. Should've worked in either. Normally we have "works in IE but doesnt work in mozilla" guess you are the one going about the other way :-). Would updating ie to the latest patches be a solution ?
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12166804
I will try a couple things and see where it leads me. I will update you guys tomorrow.

Thanks for all your help.
0
 
LVL 6

Expert Comment

by:ren_b
ID: 12168747
> That will take the focus off the textarea as soon as they click on it.
>> I cannot have the textarea loose focus.

huh? what is it you're trying to accomplish with this? why do mouse clicks have to be disabled? are you sure this doesn't work in IE - maybe javascript was turned off? this works in IE 5.2:mac, safari, and firefox...

   <textarea onmousedown="return false"></textarea>
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12187648
I tested the following sample on several different pc's with different versions of IE and it still does not block the onMouseDown event.

<textarea onMouseDown="return false;">Some text and some more text.</textarea>

Unless anybody have any other ideas, I'm going to close this out.

Let me know who you guys feel the points should go to.

Thanks again for all your help.
0

Featured Post

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

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

734 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