I'm using Robert Nyman's script to get all elements with same class in document, but it doesn't work with onclick or any other event:
var photo = document.getElementsByClassName("photo_class","img",document.getElementById("photo_wrap"));
photo.onclick = function(){alert("Finaly!");
Maybe you know how to fix it? Thanks!
Document.getElementsByClassName() The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node.
getElementsByClassName("a") will reliably list them in order: d1, d2, d3, d4, d5.
The getElementsByClassName() method returns an HTMLCollection.
The getElementsByClassName() method returns an array-like of objects of the child elements with a specified class name. The getElementsByClassName() method is available on the document element or any other elements. The method returns the elements which is a live HTMLCollection of the matches elements.
I guess photo is a array. If that's the case, try that:
var photo = document.getElementsByClassName(
"photo_class","img",document.getElementById("photo_wrap")
);
for (var i=0; i < photo.length; i++) {
photo[i].onclick = function(){
alert("Finaly!");
}
};
Try
photo[0].onclick = function(){alert("Finaly!");};
getElementsByClass returns array
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