Just grab the element by its id using getElementById and then just use .src to get image src JavaScript. Image source can URL format.
1 |
var imgsrc = document.getElementById("imageId").src; |
Example: How to JavaScript get image source
Here is an HTML example code of getting src of the image in JS. We are using variables to store source value.
1 2 3 4 5 6 7 8 9 10 11 12 |
<!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:

Let’s us know if you know any other way or have better example. You can do also comment any suggestions.
Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version