How to set href attribute at runtime?

We know how to set the href attribute of the anchor tag int HTML, but sometimes we may require to set the the href attribute at runtime i.e. for example, when a user provides us a url and we want to set it at runtime. We can do this with the help of jQuery.
 

Example 1:  In this example, using jquery, we set the attr() method to the url entered by the user in the input tag, when user clicks the set href buton. 

html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" 
              content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <script src=
        </script>
        <title>Set href attribute at runtime</title>
        <style>
            #btn {
                background-color: #4caf50;
                color: white;
            }
            input {
                color: #4caf50;
            }
            a {
                color: #4caf50;
            }
            h1 {
                color: green;
            }
        </style>
    </head>
    <body>
        <center>
            <h1>GeeksforGeeks</h1>
            <b>Set href attribute at runtime</b>
            <br>
            <input type="text" name="url" />
            <button id="btn">Set href</button>
            <button>
                <a id="click" href="#" 
                   target="_blank">
                    link
                </a>
           </button>
        </center>
    </body>
  
    <script>
        $(document).ready(function () {
            $("#btn").click(function () {
                $("#click").attr("href", 
                $('input[name$="url"]').val());
            });
        });
    </script>
</html>

chevron_right


Output:

Example 2:  In this example, we replace the anchor tag in the div ‘link’ with another anchor tag to change the href value. This is another way in which we can change the value of the href attribute. 

html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport"
              content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" 
                   content="ie=edge" />
        <script src=
        </script>
        <title>Set href attribute at runtime</title>
        <style>
            #btn {
                background-color: #4caf50;
                color: white;
            }
            input {
                color: #4caf50;
            }
            a {
                color: #4caf50;
            }
            h1 {
                color: green;
            }
        </style>
    </head>
    <body>
        <center>
            <h1>GeeksforGeeks</h1>
            <b>Set href attribute at runtime</b>
            <br>
            <div id="link">
                <a id="click" href=
                     target="_blank">
                  https://practice.geeksforgeeks.org/
              </a>
              <button id="btn">Change url</button>
            </div>
        </center>
    </body>
  
    <script>
        $(document).ready(function () {
            $("#btn").click(function () {
                $("#link").html(
       https://www.geeksforgeeks.org</a>");
  alert("Url changed to https://www.geeksforgeeks.org/");
            });
        });
    </script>
</html>

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.