asp.net.ph

onclick Event

DHTML Events


Fires when the user clicks the left mouse button on the object.

Syntax


Inline HTML <element onclick = "handler" ... > All platforms
Event property object.onclick = handler ECMA-262 Language Specification
Named script <script FOR=object EVENT=onclick> Internet Explorer® only

Remarks


Bubbles Yes
Cancels Yes
To invoke
  • Click the object.
  • Invoke the click method.
  • Press ENTER in a form.
  • Press the access key for a control.
  • Select an item in a combo box or list box by clicking the left mouse button or by pressing the arrow keys and then pressing the ENTER key.
Default action Initiates any action associated with the object. For example, clicking an A object causes the browser to load the document specified by the href property. To cancel the default behavior, set the returnValue property of the event object to FALSE.

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 Properties

While event handlers in the Document Object Model do not receive parameters directly, the handler can query the event object for data.

Event Object Properties


Example

The 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>&nbsp;&nbsp;
  <span style="background:navy;color:lime">
    Click Here </span>
  &nbsp;&nbsp;<input type=button value="Click Here">
  &nbsp;&nbsp;<a href="#top">Click Here</a>
<br><br>
What's been clicked<br>
<input name=txtOutput></input>
<input name=txtOutput1></input></div>
</body>

 Show me 

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>

 Show me 

See Also

click, ondblclick




Contents
HTML Elements
HTML Element Attributes
CSS Attributes
DOM Objects
DOM Object Properties
DOM Events Reference
DOM Methods Reference
DOM Collections Reference
 

Previous page Back to top Next page

© 2000-2010 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 asp.net.ph