Hope somebody can help with this:
- How can I get element's child with exact tag name (in this case
img
)? - How can I get this child's attribute?
Next doesn't work:
var banner = document.getElementById('banner');
var imgElement = banner.getElementsByTagName('img');
var imgSrc = imgElement.getAttribute('src');
Last line returns imgElement.getAttribute is not a function
. I guess it's because of second line, where I get object HTMLCollection
...But why I got this and what I have to do to get what I want?
Thanx a lot in advance for any help.
getElementsByTagName returns an HTMLCollection, so get the first element in the array and then its src
Another solution is to use querySelector(will be little slower)