Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HTML DOM Image alt Property

  • Last Updated : 22 Oct, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

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
Recommended Articles
Page :

Start Your Coding Journey Now!