Search code examples
javascripthtmlimagesrc

Finding the src of an image via Javascript


I found the answer to my question via a simple Google search, although, there is a slight problem that I ran into.

So far, I have an image on my HTML page with the ID of newAvatar. I am attempting to find this image via its ID, and save it's source into a variable, then eventually into an XML document to be used later on.

When I save this source into a variable, or alert it for checking purposes, it is displaying the full source, which is unusual. For example, I am running my site on a WAMP server, so the variable is saving as:

http://localhost/uploads/newUploadedImage.jpg

When in reality, the source should be plain and simple, uploads/newUploadedImage.jpg

The image tag is

<img src="uploads/newUploadedImage.jpg" width="60px" height="60px;" id="newAvatar"/>

The way i am checking the source is

alert(document.getElementById('newAvatar').src);

Any idea how to get rid of the first lot of junk?

All help is greatly appreciated!


Solution

  • By using @DavidThomas comment before, I managed to get it working. This is what i changed my alert to:

    alert(document.getElementById('newAvatar').getAttribute('src'));