| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onclick EventFires when the user clicks the left mouse button on the object. Syntax
Remarks
A click event occurs when the pointing device button is clicked over an element. A click is defined as a mousedown and mouseup over the same screen location. The sequence of these events is: mousedown, mouseup, click. For example, if the user clicks the left mouse button, the onclick event for an object occurs only if the mouse pointer is over the object and both an onmousedown and an onmouseup event occur in order. if the user presses down in the object but moves the mouse pointer out of the object before releasing, no onclick event occurs. The onclick event changes the value of a control in a group. This change initiates the event for the group, not for the individual control. For example, if the user clicks a radio button or check box in a group, the onclick event occurs after the onbeforeupdate and onafterupdate events for the control group. If the user clicks an object that can receive the input focus but does not already have the focus, the onfocus event occurs for that object before the onclick event. If the user double-clicks the left mouse button in a control, an ondblclick event occurs immediately after the onclick event. Although the onclick event is available on a large number of HTML elements, if a Web page is to be accesible to keyboard users, its use should be restricted to the A, INPUT, AREA, and BUTTON elements. These elements automatically allow keyboard access through the TAB key, making Web pages that use them accessible to keyboard users. Event Object PropertiesWhile event handlers in the Document Object Model do not receive parameters directly, the handler can query the event object for data.
ExampleThe following examples demonstrate the use of onclick event handlers for a document. This example uses the event object to gain information about the origin of the click, and cancels the default action if the onclick event is fired off an anchor. <head>
<script language="JavaScript">
function clickIt ( ) {
txtOutput.value = window.event.srcElement.tagName;
txtOutput1.value = window.event.srcElement.type;
// checks if the click occurred in an anchor, then cancels the event
if (window.event.srcElement.tagName=="A"){
window.event.returnValue = false;}}
</script>
</head>
<body onclick="clickIt()">
<div align="center">
<input value="Click Here" size=10>
<span style="background:navy;color:lime">
Click Here </span>
<input type=button value="Click Here">
<a href="#top">Click Here</a>
<br><br>
What's been clicked<br>
<input name=txtOutput></input>
<input name=txtOutput1></input></div>
</body>
This example shows how to bind the onclick event to grouped controls. <head>
. . .
<script language="JavaScript">
function getOpt ( ) {
txtOutput.value = window.event.srcElement.value;
}
</script>
</head>
<body>
. . .
<div align=center>
<table>
<tr>
<td><input type=radio name=optGroup id=sex
value="Sex" onclick="getOpt()"></td>
<td><label for=sex>Sex</label></td></tr>
<tr>
<td><input type=radio name=optGroup id=drugs
value="Drugs" onclick="getOpt()"></td>
<td><label for=drugs>Drugs</label></td></tr>
<tr>
<td><input type=radio name=optGroup id=r&r
value="Rock and Roll" onclick="getOpt()"></td>
<td><label for=r&r>Rock and Roll</label></td></tr>
</table>
<p>Value of control on which the
<b>onclick</b> event has fired.</p>
<input name=txtOutput size=10></input></div>
</body>
See Also |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme