Skip to content

JavaScript get image source from img tag | HTML Example code

  • by

Just grab the element by its id using getElementById and then just use .src to get image src JavaScript. Image source can URL format.

var imgsrc = document.getElementById("imageId").src;

Example: How to JavaScript get image source

Here is an HTML example code for getting an src of the image in JS. We are using variables to store source value.

<!DOCTYPE html>
<html>
<body>

   <img src="https://tutorial.eyehunts.com//wp-content/uploads/2021/01/JavaScript-get-element-by-tag-Method.png" id="image" />

   <script>
        var imgsrc = document.getElementById("image").getAttribute('src');
        console.log(imgsrc)
    </script> 
</body>
</html>

Output:

JavaScript get image source

Let us know if you know any other way or have a better example. You can do also comment with any suggestions.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *