Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'click()' in JavaScript

Tags:

javascript

I was playing around with JavaScript in FireFox, and hit a problem that is illustrated in the following example:

<HEAD> <script type="text/javascript"> function click() {     alert("click"); }  </script> </HEAD>  <BODY>  <input type="radio" onclick="click()"> </BODY> 

When I click on the radio button, nothing happens, and no errors (in Firebug)

If I change the name of the function to do_click, and change the onclick, then I get the alert.

So question is: what is happening? click does not appear to be a reserved word, or an existing symbol

like image 407
Alan Avatar asked Feb 05 '10 01:02

Alan


People also ask

What does the onclick event handler do?

What is the onClick handler in React? The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as a button, in your app. Event names are written in camelCase, so the onclick event is written as onClick in a React app.

Which element is clicked in JavaScript?

To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked.

How do you get the ID of a clicked element by JavaScript?

The buttonPressed() callback function will have a returned event object which has all the data about the HTML element that is clicked on. To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.


2 Answers

Code within inline event handlers is scoped to the element, as if it was in a with block.

Therefore, within the onclick handler, the name click resolves to the element's click method.

This can be demonstrated by writing alert(nodeName) in the handler

like image 96
SLaks Avatar answered Sep 22 '22 17:09

SLaks


DOM elements have a native click() method.

The following will show that click is a native method:

<input type="radio" onclick="alert(click.toString())"> 

You can register your click function as the event handler as follows. Within the handler, this will refer to the HTML element.

<input type="radio" id="foo" >  function click() {     alert("click"); } document.getElementById('foo').onclick = click; 

There is an excellent series of articles about browser events at http://www.quirksmode.org/js/introevents.html

like image 33
Lachlan Roche Avatar answered Sep 21 '22 17:09

Lachlan Roche