Search code examples
javascripthtmldynamichref

Dynamic Hyperlink in HTML


I need to create a hyperlink that will change each day to incorporate the date from the day previous

Example:

For a download link on May 16th

<a href="http://www.example.com/dir/download-2020-05-15" title="Download Link">Download</a>

For a download link on May 17th

<a href="http://www.example.com/dir/download-2020-05-16" title="Download Link">Download</a>

I understand that there would probably be some script that can do this for me, but I cannot find it. Sorry if I am repeating other questions here.


Solution

  • @PatMcInnes : you can create 3 different elements of a tag like this :

    var elementCreatedOne = document.getElementById("a1”);
    
    var elementCreatedTwo = document.getElementById("a2”);
    var elementCreatedThree = document.getElementById("a3”);
    
    var todaysDate = new Date();
    var formattedDate= todaysDate.getFullYear() + '-' + (todaysDate.getMonth() + 1) + '-' + todaysDate.getDate();
    
    
    elementCreatedOne .setAttribute("href", 
        “Link1” + formattedDate);
    
    elementCreatedTwo .setAttribute("href", 
        “Link2” + formattedDate);
    
    elementCreatedThree .setAttribute("href", 
        “Link3” + formattedDate);
    

    HTML :

    <a href="" id="a1” title="Download Link">Download</a>
    <a href="" id="a2” title="Download Link">Download</a>
    <a href="" id="a3” title="Download Link">Download</a>