Solved

Disable onMouseDown

Posted on 2004-09-27
31
719 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
  • 14
  • 9
  • 6
  • +2
31 Comments
 
LVL 33

Expert Comment

by:sajuks
Comment Utility
// 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
Comment Utility
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
Comment Utility
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
 
LVL 6

Expert Comment

by:viola123
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
sajuks: I need to disable the onMouseDown and onClick events, not the context menu.
0
 
LVL 6

Expert Comment

by:viola123
Comment Utility
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
Comment Utility
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
Comment Utility
i am using ie6

viola
0
 
LVL 3

Author Comment

by:hans_larson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 3

Author Comment

by:hans_larson
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
// 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
Comment Utility
No, it does not work. I'm running IE 6.0.3790.0
0
 
LVL 33

Expert Comment

by:sajuks
Comment Utility
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
Comment Utility
i am using exactly the same IE as yours. weird~~~~~~~~~~~~~~~~~~~~~~

viola
0
 
LVL 3

Author Comment

by:hans_larson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now