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
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With