Open In App

HTML DOM Anchor download Property

Last Updated : 02 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The DOM Anchor download Property in HTML DOM is used to set or return the value of the download attribute of a link. The download attribute is used to specify the target link will download when user click.

Syntax:

  • It returns the download property.
    anchorObject.download
  • It is used to set the download property.
    anchorObject.download = filename

Property Values:

  • filename: It specifies the name of the file which has to be downloaded.

Return Value: It returns a string value which represents the name of the downloaded file.

Example 1: This example returns the download property.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>HTML DOM Anchor download Property</title>
</head>
 
<body style="text-align: center;">
    <h1>GeeksforGeeks</h1>
 
    <h2>HTML DOM Anchor download Property</h2>
 
        id="GFG" rel="nofollow" hreflang="en-us"
        target="_self" download="GFG">
        GeeksforGeeks
    </a><br><br>
 
    <button onclick="myGeeks()">
        Submit
    </button>
 
    <p id="sudo"></p>
 
    <script>
        function myGeeks() {
            let x = document.getElementById("GFG").download;
            document.getElementById("sudo").innerHTML = x;
        }
    </script>
</body>
 
</html>


Output:

anchor-download

Example 2: This example sets the Download Property.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM Anchor download Property
    </title>
</head>
 
<body style="text-align: center;">
    <h1>GeeksforGeeks</h1>
 
    <h2>DOM Anchor download Property</h2>
 
        id="GFG" rel="nofollow" hreflang="en-us"
        target="_self" download="GFG">
        GeeksforGeeks
    </a><br><br>
 
    <button onclick="myGeeks()">
        Submit
    </button>
 
    <p id="sudo"></p>
 
    <script>
        function myGeeks() {
            let x = document.getElementById("GFG")
                .download = "Geeks Home Page";
 
            document.getElementById("sudo").innerHTML
                = "The value of the download attribute " +
                "was changed to " + x;
        }
    </script>
</body>
 
</html>


Output :

anchor-download-2

Supported Browsers:

  • Google Chrome
  • Internet Explorer 10.0 +
  • Firefox
  • Opera
  • Safari


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads