Categories
JavaScript Answers

How to get img element src and set as variable with JavaScript?

Spread the love

To get img element src and set as variable with JavaScript, we get the src property.

For instance, we write

const imgSrc = document.getElementById("some-img").src;

to select the img element with getElementById.

And then we get its src attribute value with the src property.

If src attribute has a relative path, then the src property would be the resolved path.

Conclusion

To get img element src and set as variable with JavaScript, we get the src property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.