EN
Este contenido no se encuentra disponible en su idioma, pero aquí tiene la versión en inglés.

click | onclick event

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

Note  Starting with Internet Explorer 11, this event fires a MSPointerEvent object instead of MouseEvent. You can use the MouseEvent.pointerType property to determine the type of contact that the click originated from (touch, mouse, or pen).

HTML 4.01 Specification, Section 18.2.3

 

Syntax

HTML Attribute <element onclick = "handler(event)">
Event Property object.onclick = handler;
attachEvent Method object.attachEvent("onclick", handler)
addEventListener Method object.addEventListener("click", handler, useCapture)

 

Event information

SynchronousNo
BubblesYes
CancelableYes

 

Event handler parameters

pEvtObj [in]

Type: IHTMLEventObj

Standards information

Remarks

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 an onmousedown and an onmouseup event occur in that order. For example, if the user clicks the mouse on the object but moves the mouse pointer away from 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 document is to be accessible to keyboard users, you should restrict its use to the a, input, area, and button elements. These elements automatically allow keyboard access through the TAB key, making documents that use the elements accessible to keyboard users. For more information, please see the section on writing accessible Dynamic HTML.

Initiates any action associated with the object. For example, if the user clicks an a object, the client loads the document specified by the href property. To cancel the default behavior, set the returnValue property of the event object to FALSE.

To invoke this event, do one of the following:

  • Click the object.
  • Invoke the click method.
  • Press the ENTER key 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.

Examples

This example uses the event object to gain information about the origin of the click. In addition, it cancels the default action to prevent navigation of anchor elements, unless the SHIFT key is pressed. Normally a Shift+Click opens the target of a link in a new window; however, the script replaces the current document by setting the location of the window object.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onclickEX.htm


<script type="text/javascript">
/* This code cancels the event. If the click occurs in an anchor
   and the SHIFT key is down, the document is navigated. */
function clickIt()  
{
    var e = window.event.srcElement
    txtName.value = e.tagName;
    txtType.value = e.type;
    if ((e.tagName == "A") && 
        (window.event.shiftKey)) {
        window.location.href = e.href;
    }
    
    window.event.returnValue = false; 
}
</script>
<body onclick="clickIt()">
<p>To follow a link, click while pressing the SHIFT key.</p>
<a href="about:blank">Click Here</a>
<textarea name="txtName"></textarea> <textarea name="txtType"></textarea>
</body>

This example shows how to bind the onclick event to grouped controls.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onclickEX1.htm


<head>
<script type="text/javascript">
function CookieGroup() 
{
txtOutput.value = window.event.srcElement.value;
}
</script>
</head>
<body>
<!-- Controls are grouped by giving them the same NAME but unique IDs. -->
<p>Grouped Radio Buttons<br>
<input type="radio" 
    name="rdoTest" 
    id="Cookies" 
    value="accept_cookies" 
    checked 
    onclick="CookieGroup()"><br>
<input type="radio" 
    name="rdoTest" 
    id="NoCookies" 
    value="refuse_cookies" 
    onclick="CookieGroup()"><br>
</p>
<p>Ungrouped Radio Button<br>
<input type="radio" 
    name="rdoTest1" 
    value="chocolate-chip_cookies" 
    onclick="CookieGroup()"><br>
</p>
<p>Value of control on which the onclick event has fired<br>
<textarea name="txtOutput" style="width: 250px"></textarea> </p>
</body>

See also

a
abbr
acronym
address
applet
area
b
bdo
big
blockQuote
body
button
caption
center
cite
code
custom
dd
dfn
dir
div
dl
document
dt
em
embed
fieldSet
font
form
hn
hr
i
img
input type=button
input type=checkbox
input type=email
input type=file
input type=image
input type=number
input type=password
input type=radio
input type=range
input type=reset
input type=search
input type=submit
input type=tel
input type=text
input type=url
kbd
label
legend
li
listing
map
marquee
menu
nextID
noBR
object
ol
p
plainText
pre
rt
ruby
s
samp
select
small
span
strike
strong
sub
sup
SVGSVGElement
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
window
xmp
click
onmsgesturetap

 

 

Mostrar:
© 2014 Microsoft