How to change the href value of a tag after click on button using JavaScript ? Last Updated : 07 Feb, 2023 Comments Improve Suggest changes Like Article Like Report JavaScript is a high-level, interpreted, dynamically typed, and client-side scripting language. HTML is used to create static web pages. JavaScript enables interactive web pages when used along with HTML and CSS. Document Object Manipulation (DOM) is a programming interface for HTML and XML documents. DOM acts as an interface between JavaScript and HTML combined with CSS. The DOM represents the document as nodes and objects i.e. the browser turns every HTML tag into a JavaScript object that we can manipulate. DOM is an object-oriented representation of the web page, that can be modified with a scripting language such as JavaScript. To manipulate objects inside the document we need to select them and then manipulate them. Selecting an element can be done in five ways: document.querySelector() Method: It returns the first element that matches the query.document.querySelectorAll() Method: It returns all the elements that match the query.document.getElementById() Method: It returns the one element that matches the id.document.getElementsByClassName() Method: It returns all the elements that match the class.document.getElementsByTagName() Method: It returns a list of the elements that match the tag name. DOM allows attribute manipulation. Attributes control the HTML tag's behavior or provide additional information about the tag. JavaScript provides several methods for manipulating an HTML element attribute. The following methods are used to manipulate the attributes: getAttribute() method: It returns the current value of an attribute on the element and returns null if the specified attribute does not exist on the element.setAttribute() method: It updates the value of an already existing attribute on the specified element else a new attribute is added with the specified name and value.removeAttribute() method: It is used to remove an attribute of the specified element. Example 1: The below code demonstrates the attribute manipulation where the href attribute of <a> tag changes on button click. A function is called on button click which updates the attribute value. The function myFunction() is a JavaScript function and it makes the HTML code more interactive by making runtime modifications. html <!DOCTYPE html> <html> <head> <title> How to change the href attribute dynamically using JavaScript? </title> </head> <body style="text-align:center;"> <h1 style="color:green"> GeeksforGeeks </h1> <h3> Change the href attribute value<br> dynamically using JavaScript </h3> <a href="https://www.google.com"> Go to Google </a> <br><br> <button onclick="myFunction()"> Click Here! </button> <script type="text/javascript"> function myFunction() { var link = document.querySelector("a"); link.getAttribute("href"); link.setAttribute("href", "https://www.geeksforgeeks.org"); link.textContent = "Go to GeeksforGeeks"; } </script> </body> </html> Output: tag after click on button using JavaScript ?">How to change the href value of <a> tag after click on button using JavaScript ? Explanation: The link opens https://www.google.com before the button is clicked. when the button is clicked then the function myFunction() is called which selects the href attribute of <a> tag and updates its value to https://www.geeksforgeeks.org, Since there is only one <a> tag in the HTML document and we aim to change its attribute value, we use querySelector() and the attribute is updated using setAttribute() method. Example 2: The link opens https://www.google.com before the button is clicked. When the button is clicked the function myFunction() is called which selects the href attribute of <a> tag and updates its value to https://www.geeksforgeeks.org. In this approach, the getElementById() method is used to select the element whose destination URL is to be changed. html <!DOCTYPE html> <html> <head> <title> How to change the href attribute dynamically using JavaScript? </title> </head> <body style="text-align:center;"> <h1 style="color:green"> GeeksforGeeks </h1> <h3> Change the href attribute value<br> dynamically using JavaScript </h3> <a href="https://www.google.com" id="myLink"> Go to Google </a> <br><br> <button onclick="myFunction()"> Click Here! </button> <script type="text/javascript"> function myFunction() { document.getElementById('myLink').href ="https://www.geeksforgeeks.org"; document.getElementById("myLink") .textContent = "Go to GeeksforGeeks"; } </script> </body> </html> Output: tag after click on button using JavaScript ?">How to change the href value of <a> tag after click on button using JavaScript ? Comment More infoAdvertise with us Next Article How to change the href value of a tag after click on button using JavaScript ? S Shreyasi_Chakraborty Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Change the Button Label when Clicked using JavaScript ? Changing the Label of the Button element when clicked in JavaScript can be used to provide more information to the user such as the text of the Submit button will change to the Submitted as soon as the form submission is completed. The below approaches can be used to accomplish this task: Table of C 2 min read How to change a button text on click using localStorage in Javascript ? In this article, we will learn how to change a button text using Javascript localStorage when we click on the button achieved by adding HTML onclick event listener. Approach: HTML DOM Window localStorage allows us to store key-value pairs in our browser using an object. The name of the key should be 3 min read How to change the text and image by just clicking a button in JavaScript ? The image and text can be changed by using JavaScript functions and then calling the functions by clicking a button. We will do that into 3 sections., in the first section we will create the structure by using only HTML in the second section we will design minimally to make it attractive by using si 2 min read How to Change the ID of Element using JavaScript? We are given an element and the task is to change the ID of elements using JavaScript. ID is unique for any element and it can be assigned to an element only once. JavaScript provides a method to access this id and also to manipulate the id. Syntax:Selected_element.id = newID;Below are the appraoche 2 min read How to Change Text Inside all HTML Tags using JavaScript ? In this article, we will learn how to change the text content inside all HTML tags using JavaScript. This skill is valuable when you need to dynamically modify the text displayed on a web page. which can be useful for various scenarios like updating content, internationalization, or creating dynamic 3 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 How to Change the src Attribute of an Image Element in JavaScript / jQuery? To change the src attribute of an image element in JavaScript or jQuery, you can dynamically update the image by setting a new value for the src attribute.Below are the methods to change the src attribute of an image element:Table of ContentUsing JavaScript DOM methodsUsing jQuery prop() MethodUsing 2 min read How to set location and location.href using JavaScript ? In this article, we will set the location and location.href using Javascript.Both location and location.href are used to set or return the complete URL of your current page. They return a string that contains the entire URL with the protocol.Syntax:location = "https://www.geeksforgeeks.org";orlocati 1 min read How to Change Link Color onClick and Keep It using JavaScript? To change the color of a link on click and keep it that way using JavaScript, you can modify its style or apply a CSS class. Here's how you can achieve this:Example: Changing Link Color on Click and Keeping ItHTML StructureHTML<a href="#" class="link" id="myLink">Click me</a> <style 2 min read How to Open a Link Without Clicking on it using JavaScript? To open a link without clicking on it we can use the onmouseover method of JavaScript. The link will open when the mouse moves over the text. It returns a newly created window, or NULL if the call gets failed. Syntax:window.open( URL, name, Specs )Note: Allow Pop-up of Web Browser. Example 1: URL is 1 min read Like