Search code examples
javascriptonclickgetelementbyid

How can I find an element by onclick attribute value?


I am wondering if it is possible to find an element by the onclick function?

For example:

<a class = "calculator" onclick = add(number1,number2);

<a class = "calculator" onclick = add(number2, number3);

And I want to get the first one and the only difference is the onclick function so I thought thats the way I could differ them. Looks so far like this:

var elements = document.getElementsByClassName("calculator");

console.log(elements);

for (var i = 0; i <  elements .length; ++i) {
elementsSpecific = elements[i]. //The missing part

console.log(delementsSpecific);
 }

Solution

  • You can get the onclick function by using

    elements[i].getAttribute('onclick');