How to Change Link Color onClick and Keep It using JavaScript? Last Updated : 14 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 Structure HTML <a href="#" class="link" id="myLink">Click me</a> <style> .active-link { color: red; /* Change this to your desired color */ } </style> <script> document.getElementById('myLink').addEventListener('click', function(event) { event.preventDefault(); // Prevent default behavior if needed // Remove the active-link class from all links if you want only one to be active const links = document.querySelectorAll('.link'); links.forEach(link => link.classList.remove('active-link')); // Add the active-link class to the clicked link this.classList.add('active-link'); }); </script> Output:OutputOn Clicking the LinkExplanation:HTML Structure:A simple <a> tag with an id and class. The class="link" is used to group multiple links if needed.CSS Class for Active State:.active-link sets the desired color for the clicked link. You can customize the color as per your requirement.JavaScript Logic:An event listener is attached to the link. When the link is clicked, the active-link class is added to it.event.preventDefault() prevents the default link behavior (navigation) if necessary.To ensure only one link remains active, you can first remove the active-link class from all links and then add it to the clicked link.Handling Multiple LinksIf you have multiple links and want to keep track of which one was last clicked, you can use a similar approach by applying the event listener to all links with a common class: HTML <a href="#" class="link">Link 1</a> <a href="#" class="link">Link 2</a> <a href="#" class="link">Link 3</a> <script> document.querySelectorAll('.link').forEach(link => { link.addEventListener('click', function(event) { event.preventDefault(); // Prevent default behavior if needed // Remove the active-link class from all links document.querySelectorAll('.link').forEach(link => link.classList.remove('active-link')); // Add the active-link class to the clicked link this.classList.add('active-link'); }); }); </script> Output:OutputSummary:The active-link class is used to change and persist the color.The addEventListener() method handles clicks on links.To handle multiple links, loop through all elements and manage the class application accordingly.This approach ensures that the clicked link keeps its new color until another link is clicked Comment More infoAdvertise with us Next Article How to Change Link Color onClick and Keep It using JavaScript? A anuragtriarna Follow Improve Article Tags : JavaScript Web Technologies Web QnA Similar Reads How to randomly change image color using HTML CSS and JavaScript ? In this article, we will create a webpage where we can change image color randomly by clicking on the image using HTML, CSS, and JavaScript. The image color will change randomly using the JavaScript Math random() function.Approach:First of all select your image using HTML <img> tag.In JavaScri 2 min read How to Change Background Color of a Div on Mouse Move Over using JavaScript ? The background color of the div box can be easily changed using HTML, CSS, and Javascript. We will use the querySelector() and addEventListener() methods to select the element and then apply some math logic to change its background color. The below sections will guide you on how to create the effect 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 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 color depending on background color using JavaScript? The task is to set the foreground color based on the background color so that it will become visible. Here few of the important techniques are discussed. We are going to use JavaScript. Approach: First, select the random Background color(by selecting random RGB values) or a specific one.Use the YIQ 3 min read Like