I know that getElementsByTagName
and getElementsByClassName
need an index identifier in order for the objects to be bound to an event listener.
So the question is, how do I add an event listener to a collection of HTML elements found using getElementsByTagName
or getElementsByClassName
?
<input class="inputs" type="submit" value="Hello" />
<input class="inputs" type="submit" value="World" />
var inputElem = document.getElementsByTagName('input');
inputElem.addEventListener('click', function(){
alert(this.value);
}, false);
I know how to do this in jQuery, but I want to know how to do it with pure JS.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…