Open In App

HTML DOM Image src Property

The HTML DOM Image src Property is used to set or returns the value of the src attribute of the <img> element. This attribute is used to specify the URL of the Image.

Syntax:



Property Values: It contains a single value URL that specifies the URL of the document that is embedded in the Image. There are two types of URL links which are listed below:

Return value: It returns a string value that represents the URL of the image.

Example: Below code returns the src Property.




<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color: green">
            GeeksforGeeks
        </h1>
  
        <h2>HTML DOM Image src Property</h2>
  
        <img id="GFG" src=
             width="400" height="150" />
        <br>
  
        <button onclick="Geeks()">
            Click me!
        </button>
  
        <p id="sudo"></p>
  
    </center>
  
    <script>
        function Geeks() {
            var g = document.getElementById("GFG").src;
            document.getElementById("sudo").innerHTML = g;
        }
    </script>
</body>
  
</html>

Output:

Example 2: This example sets the src property.




<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color: green">GeeksforGeeks</h1>
        <h2>HTML DOM Image src Property</h2>
        <img id="GFG" src=
             width="400" height="150" />
        <br>
        <button onclick="Geeks()">Click me!</button>
        <p id="sudo"></p>
  
    </center>
  
    <script>
        function Geeks() {
          var g = document.getElementById("GFG").src =
            document.getElementById("sudo").innerHTML = g;
        }
    </script>
  
</body>
  
</html>

Output:

Supported Browsers:


Article Tags :