First, I would have created this example in JSFiddle but they are in read-only mode.
I would like to get the specific element that was clicked based on a class.
var button = document.getElementsByClassName("mybutton");
button.onclick = function() {
//how do I reference the specific button that was clicked?
};
<button class="myclass">Button 1</button>
<button class="myclass">Button 2</button>
No jQuery answers please; that is not an option here.
it’s not
document.getElementByClassName, it’sdocument.getElementsByClassName.See the difference yet?
It’s easy to overlook:
The former doesn’t exist unless you define it, the latter only works in modern browsers.
getElementsByClassNamewill return a node list, which you need to iterate over to attach event listeners to each node.