Why does event.button always = 0

J

joe.osowski

I've been staring at this a while, and I haven't had much luck with it.
So I figure I'll try Usenet.

No matter what I try, it seems the event.button property is always "0".


Here is my test case HTML, can anyone tell me what I am doing wrong?

Thanks in advance.

<HTML>
<script language="javascript">

function log()
{
debugWrite("button: " + event.button);
debugWrite("keyCode: " + event.keyCode);
debugWrite("type: " + event.type);
}


function debugWrite(testString) {
document.all.debug.value = document.all.debug.value + (new
Date()).getHours() + ":" + (new Date()).getMinutes() + ":" + (new
Date()).getSeconds() + ":" + (new Date()).getMilliseconds() + " - " +
testString + "\r\n";
}

</script>

</HEAD>
<body class="gradient" bottomMargin="0" leftMargin="0" topMargin="0"
rightMargin="0" MS_POSITIONING="FlowLayout"
onload="document.all.debug.value='';">
<form>
<input type="text" name="test" id="test" size="20" onClick="log();"
onFocus="log();" onKeyDown="log();">
<input type="button" name="test" id="button" size="20"
onClick="log();" onFocus="log();" onKeyDown="log();">
<br>
<br>
<textarea id="debug" style="width:700px; height:500px"></textarea>
</form>
</body>
</HTML>
 
S

Stephen Chalmers

I've been staring at this a while, and I haven't had much luck with it.
So I figure I'll try Usenet.

No matter what I try, it seems the event.button property is always "0".

Given that a click occurs when the button is released, that may not be
unreasonable. That said, under Opera it is 1.
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Fri, 30 Sep 2005 11:44:33, seen in (e-mail address removed) posted :
function debugWrite(testString) {
document.all.debug.value = document.all.debug.value + (new
Date()).getHours() + ":" + (new Date()).getMinutes() + ":" + (new
Date()).getSeconds() + ":" + (new Date()).getMilliseconds() + " - " +
testString + "\r\n";
}

AFAIK, this does not address the problem that you are asking about : but
new Date() should be called only once to determine a date/time, since
it changes continually.

function debugWrite(testString) { var D = new Date()
document.all.debug.value +=
D.getHours() + ":" + D.getMinutes() + ":" + D.getSeconds() + ":" +
D.getMilliseconds() + " - " + testString + "\r\n";
}

function debugWrite(testString) { with (new Date())
document.all.debug.value +=
getHours() + ":" + getMinutes() + ":" + getSeconds() + ":" +
getMilliseconds() + " - " + testString + "\r\n";
}

For public use, the third colon should be a decimal point, and the
fields should have leading zeroes if small.

Code posted to News should not be wrapped by the posting agent; it
should be posted in directly executable form. It helps to use two
spaces, rather than tab, to indent. See newsgroup Wednesday FAQ.
 
G

Gérard Talbot

(e-mail address removed) a écrit :
I've been staring at this a while, and I haven't had much luck with it.
So I figure I'll try Usenet.

No matter what I try, it seems the event.button property is always "0".


Here is my test case HTML, can anyone tell me what I am doing wrong?

Thanks in advance.

<HTML>
<script language="javascript">

Language is deprecated. Type has replaced language and is
backward-compatible too. So, best is

<script type="text/javascript">


function log()
{
debugWrite("button: " + event.button);
debugWrite("keyCode: " + event.keyCode);
debugWrite("type: " + event.type);
}


function debugWrite(testString) {
document.all.debug.value = document.all.debug.value + (new
Date()).getHours() + ":" + (new Date()).getMinutes() + ":" + (new
Date()).getSeconds() + ":" + (new Date()).getMilliseconds() + " - " +
testString + "\r\n";


You can safely replace document.all with document.getElementById if you
want to support all modern browsers and MSIE 5+.

document.getElementById("debug").value
will do the trick.

http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom_access

}

</script>

</HEAD>
<body class="gradient" bottomMargin="0" leftMargin="0" topMargin="0"
rightMargin="0" MS_POSITIONING="FlowLayout"

All these attributes are proprietary ones, not recognized by non-MSIE
agents.
onload="document.all.debug.value='';">
<form>
<input type="text" name="test" id="test" size="20" onClick="log();"
onFocus="log();" onKeyDown="log();">
<input type="button" name="test" id="button" size="20"
onClick="log();" onFocus="log();" onKeyDown="log();">
<br>
<br>
<textarea id="debug" style="width:700px; height:500px"></textarea>
</form>
</body>
</HTML>




"button of type unsigned short, readonly
During mouse events caused by the depression or release of a mouse
button, button is used to indicate which mouse button changed state. The
values for button range from zero to indicate the left button of the
mouse, one to indicate the middle button if present, and two to indicate
the right button."
http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-MouseEvent-button

So, if you mouse-click inside the textarea with the left mouse button,
then it should be zero. If you type in the textarea, then event.button
should be zero the last recorded value of button.

Gérard
 
G

Gérard Talbot

Stephen Chalmers a écrit :
Given that a click occurs when the button is released, that may not be
unreasonable. That said, under Opera it is 1.

It is 0 with left mouse button click.

Opera 8.5 is DOM 2 Events compliant. See for yourself:

http://www.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/MouseEvent.htm

Nota bene: you need to allow right-click in
Tools/Preferences.../Advanced tab/Content category/Javascript Options...
button/Allow right-click scripting checkbox must be checked

Gérard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top