I have a page with the following code:
<a id="test" href="someurl" onclick="somefunction">link</a>
I need to actually read the 'onclick' data. As such, I would like something to the effect of
alert(document.getElementById('test').onClick)
Sadly, it returns undefined
. What do I need to do to get somefunction
?
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.
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. Here is the HTML for the examples in this article.
The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element.
An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.
Assuming the tag is well-formed, a tag's attribute can be obtained via Element.getAttribute
.
document.getElementById('test').getAttribute('onclick')
// -> "somefunction"
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