HTML DOM Image src Property
THE WORLD'S LARGEST WEB DEVELOPER SITE

Image src Property

❮ Image Object

Example

Change the URL of an image:

document.getElementById("myImg").src = "hackanm.gif";
Try it Yourself »

Definition and Usage

The src property sets or returns the value of the src attribute of an image.

The required src attribute specifies the URL of an image.

Note: The src property can be changed at any time. However, the new image inherits the height and width attributes of the original image, if not new height and width properties are specified.


Browser Support

Property
src Yes Yes Yes Yes Yes

Syntax

Return the src property:

imageObject.src

Set the src property:

imageObject.src = URL

Property Values

Value Description
URL Specifies the URL of the image.

Possible values:
  • An absolute URL - points to another web site (like src="http://www.example.com/default.htm")
  • A relative URL - points to a file within a web site (like src="default.htm")


Technical Details

Return Value: A String, representing the URL of the image. Returns the entire URL, including the protocol (like http://)

More Examples

Example

Return the URL of an image:

var x = document.getElementById("myImg").src;
Try it Yourself »

Related Pages

HTML reference: HTML <img> src attribute


❮ Image Object