Open In App

HTML DOM Image alt Property

The HTML DOM Image alt Property is used to set or return the value of the alt attribute of the image element. The alt attribute is used to specify the alternate text for an image. It is useful when the image is not displayed. It is used to give alternative information for an image.

Syntax:



It returns the image alt property

ImageObject.alt

It sets the Image alt property.



ImageObject.alt = text

Property Values: It contains a single value that specifies the alternative text for the image. 

Return Value: It returns a string value that represents the alternate text for the image.

Example 1: This example returns the image alt property. 




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

Output:

Example 2: This example sets the Image alt Property.




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

Output:


Article Tags :