DOM:element.onclick
From MDC
Contents |
[edit] Summary
The onclick property returns the onClick event handler code on the current element.
[edit] Syntax
element.onclick = functionRef;
where functionRef is a function - often a name of a function declared elsewhere or a function expression. See Core JavaScript 1.5 Reference:Functions for details.
[edit] Example
<html>
<head>
<title>onclick event example</title>
<script type="text/javascript">
function initElement()
{
var p = document.getElementById("foo");
// NOTE: showAlert(); or showAlert(param); will NOT work here.
// Must be a reference to a function name, not a function call.
p.onclick = showAlert;
};
function showAlert()
{
alert("onclick Event detected!")
}
</script>
<style type="text/css">
<!--
#foo {
border: solid blue 2px;
}
-->
</style>
</head>
<body onload="initElement()";>
<span id="foo">My Event Element</span>
<p>click on the above element.</p>
</body>
</html>
Or you can use an anonymous function, like this:
p.onclick = function() { alert("moot!"); };
[edit] Notes
The click event is raised when the user clicks on an element. The click event will occur after the mousedown and mouseup events.
Only one onclick handler can be assigned to an object at a time with this property. You may be inclined to use the addEventListener method instead, since it is more flexible and part of the DOM Events specification.
[edit] Specification
Not part of specification.