Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: get element's current "onclick" contents

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?

like image 333
Mala Avatar asked Jul 15 '10 08:07

Mala


People also ask

How do you get the ID of an element in JavaScript from a 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.

How do I find which element was clicked?

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.

How click event works in JavaScript?

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.

How click event works?

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.


1 Answers

Assuming the tag is well-formed, a tag's attribute can be obtained via Element.getAttribute.

document.getElementById('test').getAttribute('onclick')
// -> "somefunction"
like image 137
Faisal Avatar answered Sep 21 '22 06:09

Faisal