Image src attribute

Definition and usage

src Attribute sets or returns the image's src attribute value.

Required src attribute Specifies the URL of the image.

Tip:You can change the src attribute at any time. However, if new height and width attributes are not specified, the new image will inherit the height and width attributes of the original image.

Example

Example 1

Change the image URL:

document.getElementById("myImg").src = "wuhan.jpg";

Try it yourself

Example 2

Return the image URL:

var x = document.getElementById("myImg").src;

Try it yourself

Syntax

Return the src attribute:

imageObject.src

Set the src attribute:

imageObject.src = URL

Attribute value

Value Description
URL

Specifies the URL of the image.

Possible values:

  • Absolute URL - points to another website (such as src="http://www.example.com/default.htmls")
  • Relative URL - Points to a file within the website (such as src="default.html").

Technical Details

Return Value: A string value representing the URL of the image. Returns the entire URL, including the protocol (such as http://).

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support

Related Pages

HTML Reference Manual:HTML <img> src Attribute