Javascript - Capture src by class name

2

How to capture the src attribute of an image with a class name x?

For example:

var Image = document.getElementByClassName("Picture");
console.log(Image.src)
    
asked by anonymous 08.11.2015 / 17:26

1 answer

3

It can be done this way:

var image = document.getElementsByClassName('Picture');
console.log(image[0].src);

What you're wrong is calling the getElementByClassName function that is misspelled missing the 's' from 'Elements', and the fact that this function returns an array with all the elements it finds with the specified class name in the past argument, so also instead of capturing the value of the src src of the image was trying to access a src property of the array, where this property does not exist.

    
08.11.2015 / 19:10