Javascript - Capture src by class name

Asked

Viewed 107 times

2

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

for example:

var Image = document.getElementByClassName("Picture");
console.log(Image.src)

1 answer

3


It can be done that way:

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

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

  • Thank you so much!! Helped me a million!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.