Skip to content
Related Articles
Open in App
Not now

Related Articles

HTML DOM Image alt Property

Improve Article
Save Article
Like Article
  • Last Updated : 22 Oct, 2021
Improve Article
Save Article
Like Article

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. 

HTML




<!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:

image alt

Example 2: This example sets the Image alt Property.

HTML




<!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:


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!