How to change href attribute of a hyperlink using jQuery ? Last Updated : 12 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 conditions.Table of ContentUsing the attr() methodUsing the prop() methodUsing the attr() methodThe attr() method in jQuery is used to set or return the attributes and their values associated with the selected element. We can directly pass the href attribute and the new URL value to this method as parameters to change the URL.Syntax:$(selector).attr('href', 'newURL');Example: In this example, we will change the href attribute value using the attr() method and change the display text using the jQuery text() method. HTML <!DOCTYPE> <html> <head> <title> How to change href attribute of a hyperlink using jQuery? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <style> body { text-align: center; } h1 { color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h3> How to change href attribute of <br>a hyperlink using jQuery? </h3> <a href="https://www.geeksforgeeks.org/" target="_blank" id="GFG"> https://www.geeksforgeeks.org/ </a> <br><br> <button>Change URL</button> <script> $(document).ready(function () { $("button").click(function () { $("a").attr("href", "https://www.geeksforgeeks.org/web-development/"); $("#GFG").text( "https://www.geeksforgeeks.org/web-development/"); }); }); </script> </body> </html> Output:Using the prop() methodThe prop() method can also be used to change the URL of the href attribute by passing the href property and the new URL value as parameters in the same way we did with the attr() method in the last example.Syntax:$(selector).prop('href', 'newURL');Example: The below example will explain the use of the prop() method to change the URL of the href attibute. HTML <!DOCTYPE> <html> <head> <title> How to change href attribute of a hyperlink using jQuery? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <style> body { text-align: center; } h1 { color: green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h3> How to change href attribute of <br>a hyperlink using jQuery? </h3> <a href="https://www.geeksforgeeks.org/" target="_blank" id="GFG"> https://www.geeksforgeeks.org/ </a> <br><br> <button>Change URL</button> <script> $(document).ready(function () { $("button").click(function () { $("a").prop("href", "https://www.geeksforgeeks.org/web-development/"); $("#GFG").text( "https://www.geeksforgeeks.org/web-development/"); }); }); </script> </body> </html> Output: How to change href attribute of a hyperlink using jQuery ? Comment More infoAdvertise with us Next Article How to Add Attributes to an HTML Element in jQuery? B blalverma92 Follow Improve Article Tags : HTML jQuery-Methods HTML-Questions jQuery-Questions Similar Reads How to find all links with an hreflang attribute using jQuery ? In this article, we will learn to find all links with a hreflang attribute. The hreflang is an attribute that tells search engines the relationship between pages in different languages on your website. To find all links with an hreflang attribute using jQuery, you can use the attribute selector $('[ 2 min read How to retrieve attribute of an HTML tag using jQuery ? In this article, we will learn how do you retrieve attributes of an HTML tag using jQuery. We can use JQuery to make this done in a few lines of code. Here we are going to build an image zooming system where will see how can an attribute of the HTML tag can be retrieved and changed. Using the attr() 2 min read How to Add Attributes to an HTML Element in jQuery? To add attributes to an HTML element in jQuery, the attr() method can be used. This allows to set one or more attributes for a selected element, such as id, class, src, and href.Approach: Given an HTML document containing <label>, <input>, and <button> elements. The <input> e 1 min read How to get custom element attribute data in jQuery ? Apart from attributes, we can also use custom element attributes. In this article, we will learn how to get the value of a custom attribute in an HTML element using jQuery. What is Attribute? The HTML attribute provides information about the element. It decides the behavior of an element. Examples o 3 min read How to add a title in anchor tag using jQuery ? In this article, we will see how to add a title in the anchor tag using jQuery. The title property is used to specify extra information about the element. When the mouse moves over the element then it shows the information. The prop() method is used to add properties and values to the element using 2 min read How to add a class on click of anchor tag using jQuery ? In this article, we will see how to add a class on click of an anchor tag using jQuery. To add a class on click of the anchor tag, we use the addClass() method. The addClass() method is used to add more properties to each selected element. It can also be used to change the property of the selected e 2 min read Like