event handling

M

mehdi_mousavi

Hi folks,
Consider this html snippet:

<form name='myForm'>
<input type='button' value='go' name='myButton'>
</form>

I need to handle the onclick event of myButton control in javascript.
However, this has to be done without adding the onclick event to the
input tag above. I know there's a notation that lets me do this,
something like this:

<script>
myForm.myButton.onclick = function()
{
alert('this is executed if the user presses mybutton.');
}
</script>

but I cannot remember it exactly. Moreover, I would like to know if
this is W3C compliant.

Any help would be highly appreciated,

Cheers.
Mehdi
 
G

Gérard Talbot

mehdi_mousavi wrote :
Hi folks,
Consider this html snippet:

<form name='myForm'>

action is a required attribute

http://www.w3.org/TR/html401/interact/forms.html#edef-FORM
<input type='button' value='go' name='myButton'>

</form>

I need to handle the onclick event of myButton control in javascript.
However, this has to be done without adding the onclick event to the
input tag above. I know there's a notation that lets me do this,
something like this:

<script>

type is required:
http://www.w3.org/TR/html401/interact/scripts.html#edef-SCRIPT
myForm.myButton.onclick = function()

You can not get a scriptable access to a document element with its name
attribute; at least, not the way you do this. Your code is IE-specific.
How to get scripting access to that button?

All is explained here:

Accessing Elements with the W3C DOM
http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom_access

You can do this with
document.forms["myForm"].myButton.onclick = function () {...};

or

document.forms["myForm"].elements["myButton"].onclick = function () {...};
{
alert('this is executed if the user presses mybutton.');
}
</script>

but I cannot remember it exactly. Moreover, I would like to know if
this is W3C compliant.

If you want to make your code entirely DOM 2 compliant, you can register
an event listener on that button.

....
<script type="text/javascript">
function init()
{
if(document.addEventListener) // DOM 2 Events compliant
{
document.forms["myForm"].elements["myButton"].addEventListener("click",
ButtonClicked, false);
}
else if(window.event)
{
document.forms["myForm"].elements["myButton"].onclick = ButtonClicked;
};
}

function ButtonClicked(evt)
{
alert("this is executed if the user presses mybutton.");
}
</script>
....
</head>
<body onload="init();">
....

More on all this:
Using Web Standards in Your Web Pages
http://www.mozilla.org/docs/web-developer/upgrade_2.html

Working With Events
http://www.scottandrew.com/weblog/articles/events

Gérard
 
M

Michael Winter

On 15/03/2006 16:22, Gérard Talbot wrote:

[snip]
Accessing Elements with the W3C DOM
http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom_access

You can do this with
document.forms["myForm"].myButton.onclick = function () {...};

To describe that short cut as a 'W3C web standards replacement' is wrong
(though functionally, it's well-supported). Only the latter:
document.forms["myForm"].elements["myButton"].onclick = function () {...};

is specified by the W3C HTML DOM.

[snip]
else if(window.event)
{
document.forms["myForm"].elements["myButton"].onclick = ButtonClicked;
};
}

What does a global event property have to do with assigning a function
reference to an onclick property? There is no need to feature-test.

[snip]

Mike
 
M

Michael Winter

Michael Winter wrote :
On 15/03/2006 16:22, Gérard Talbot wrote:
[snip]
document.forms["myForm"].myButton.onclick = function () {...};

To describe that short cut as a 'W3C web standards replacement' is wrong

Well, then, what is it?

A well-supported, but non-standard, mechanism. The second form - using
the elements collection - is preferred (by me, at least) and the only
one that could be described as standardized.

Mike
 
T

Thomas 'PointedEars' Lahn

Michael said:
Accessing Elements with the W3C DOM
http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom_access

You can do this with
document.forms["myForm"].myButton.onclick = function () {...};

To describe that short cut as a 'W3C web standards replacement' is wrong
(though functionally, it's well-supported). Only the latter:
document.forms["myForm"].elements["myButton"].onclick = function ()
{...};

is specified by the W3C HTML DOM.

No, it is not. `onclick' is a proprietary property; to be standards
compliant, you have to use EventTarget::addEventListener(). The latter
line is only less proprietary than the former.
[snip]
else if(window.event)
{
document.forms["myForm"].elements["myButton"].onclick = ButtonClicked;
};
}

What does a global event property have to do with assigning a function
reference to an onclick property? There is no need to feature-test.

With `onclick', there is. However, the feature test here is
ill-conceived, because it is not tested what is used later.


PointedEars
 
M

Michael Winter

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[snip]
document.forms["myForm"].elements["myButton"].onclick
= function () {...};

is specified by the W3C HTML DOM.

No, it is not. `onclick' is a proprietary property; [...]

I would have hoped that from what I quoted - particularly the first line
of that quote - it would be clear that my comment above was directed
toward the approach taken to access the element, rather than the
addition of an event listener.

[snip]

Mike
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top