How to set href attribute at runtime? Last Updated : 30 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report We know how to set the href attribute of the anchor tag int HTML, but sometimes we may require to set 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 button. html <!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= "https://code.jquery.com/jquery-2.1.1.min.js"> </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> 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 <!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= "https://code.jquery.com/jquery-2.1.1.min.js"> </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= "https://practice.geeksforgeeks.org/" 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( "<a href='https://www.geeksforgeeks.org/'> https://www.geeksforgeeks.org</a>"); alert("Url changed to https://www.geeksforgeeks.org/"); }); }); </script> </html> Output: Comment More infoAdvertise with us Next Article HTML DOM createAttribute() Method P parasmadan15 Follow Improve Article Tags : Web Technologies JQuery jQuery-Misc Similar Reads HTML <a> href Attribute The HTML <a> href attribute is used to specify the URL of the page that the link points to. When the href attribute is not present in the <a> element, it will not function as a hyperlink. This attribute is essential for creating links to any address and is used in conjunction with the 1 min read How to insert a JavaScript variable inside href attribute? To insert a JavaScript variable inside the href attribute of an HTML element, <a > ... </a> tags are used. One of the attributes of 'a' tag is 'href'.href: Specifies the URL of the page the link goes to.Below are the Methods to use Variables inside this 'href' attribute: Table of Content 3 min read HTML DOM createAttribute() Method This createAttribute() method is used to create an attribute with the specified name and returns the attribute object. The attribute.value property is used to set the value of the attribute and the element.setAttribute() method is used to create a new attribute for an element. This method() contains 2 min read HTML href Attribute HTML href Attribute: It is used to specify the URL of the document. It contains the full address of the page that the user requests. It is used to link or connect one document to another document.Supported tags:HTML <a> href AttributeHTML <base> href AttributeHTML <area> href Attri 3 min read HTML <a> target Attribute The HTML <a> target Attribute is used to specify where to open the linked document. This attribute enhances the functionality and user experience by allowing developers to control the behavior of links.Syntax<a target="_blank | _self | _parent | _top | framename"\> Attribute Values_blank 1 min read How to change href attribute of a hyperlink using jQuery ? Changing the href attribute of a hyperlink using jQuery refers to the process of modifying the URL a link directs to without altering the HTML manually. This capability is useful for creating dynamic web applications where link destinations need to change based on user interactions or other conditio 2 min read Like