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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 830
  • Last Modified:

Disable onMouseDown

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
hans_larson
Asked:
hans_larson
  • 14
  • 9
  • 6
  • +2
1 Solution
 
sajuksCommented:
// 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
 
darksinclairCommented:
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
 
hans_larsonAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
viola123Commented:
<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
 
viola123Commented:
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
 
sajuksCommented:
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
 
hans_larsonAuthor Commented:
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
 
sajuksCommented:
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
 
hans_larsonAuthor Commented:
sajuks: I need to disable the onMouseDown and onClick events, not the context menu.
0
 
viola123Commented:
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
 
hans_larsonAuthor Commented:
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
 
viola123Commented:
i am using ie6

viola
0
 
hans_larsonAuthor Commented:
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
 
viola123Commented:
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
 
hans_larsonAuthor Commented:
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
 
hans_larsonAuthor Commented:
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
 
sajuksCommented:
@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
 
hans_larsonAuthor Commented:
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
 
sajuksCommented:
// 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
 
hans_larsonAuthor Commented:
No, it does not work. I'm running IE 6.0.3790.0
0
 
sajuksCommented:
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
 
viola123Commented:
i am using exactly the same IE as yours. weird~~~~~~~~~~~~~~~~~~~~~~

viola
0
 
hans_larsonAuthor Commented:
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
 
sajuksCommented:
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
 
hans_larsonAuthor Commented:
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
 
sajuksCommented:
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
 
hans_larsonAuthor Commented:
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
 
sajuksCommented:
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
 
hans_larsonAuthor Commented:
I will try a couple things and see where it leads me. I will update you guys tomorrow.

Thanks for all your help.
0
 
ren_bCommented:
> 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
 
hans_larsonAuthor Commented:
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

Independent Software Vendors: 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!

  • 14
  • 9
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now