Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
JavaScript - Remove the href Attribute from Links Using jQuery or JS
Next article icon

JavaScript - Remove the href Attribute from Links Using jQuery or JS

Last Updated : 27 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Removing the href attribute from links using jQuery is a common task when you want to disable navigation functionality temporarily or modify link behavior dynamically. Here's how you can achieve this with simple methods.

1. Using the removeAttr Method

You can use jQuery’s removeAttr method to remove the href attribute from all or specific links.

  • Anchor tags (<a>): These are used to create a links on the page that take you to another website when clicked.
  • Button (<button>): Clicking this button removes the link from the anchor tags.
  • removeAttr method: This jQuery method remov href (link) from the anchor tags, making them no longer clickable.
HTML
<!DOCTYPE html> <html lang="en"> <head> 	<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> 	<a href="https://www.geeksforgeeks.org/javascript/"         class="myLink">Go to Example</a> 	<a href="https://www.geeksforgeeks.org/react-native/"         class="myLink">Go to Another Example</a> 	<button id="removeHrefButton">Remove href</button> 	<script> 		$('#removeHrefButton').click(function () {           // Removes href attribute from all elements with class myLink 			$('.myLink').removeAttr('href');  		}); 	</script> </body> </html> 

2. Replace href with a Placeholder

Instead of removing href, replace it with a placeholder like # to indicate the link is inactive.

  • When you click the "Replace href" button, it triggers a change in the links.
  • The attr('href', '#') method replaces the current link URL with #, making the link not go anywhere.
  • After the button click, the links will still look like clickable, but they won’t take you to the original websites.
HTML
<!DOCTYPE html> <html lang="en"> <head> 	<script src= "https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> 	<a href="https://www.geeksforgeeks.org/javascript/"         class="myLink">Go to Example</a> 	<a href="https://www.geeksforgeeks.org/react-native/"        class="myLink">Go to Another Example</a> 	<button id="replaceHrefButton">Replace href</button> 	<script> 		$('#replaceHrefButton').click(function () {           // Replaces href attribute with # 			$('.myLink').attr('href', '#');  		}); 	</script> </body>  </html> 

3. Using event.preventDefault() Method

If you don't want to remove or replace the href attribute, you can prevent the default link behavior dynamically.

  • When you click the "Disable Links" button, it sets up a click handler for the links.
  • The event.preventDefault() method stops the links from going anywhere when clicked.
  • After clicking the button, clicking the links will show an alert message ("Link disabled!") instead of opening the webpage.
HTML
<!DOCTYPE html> <html lang="en"> <head> 	<script src= "https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> 	<a href="https://www.geeksforgeeks.org/javascript/"         class="myLink">Go to Example</a> 	<a href="https://www.geeksforgeeks.org/react-native/"         class="myLink">Go to Another Example</a> 	<button id="disableLinksButton">Disable Links</button> 	<script> 		$('#disableLinksButton').click(function () { 			$('.myLink').on('click', function (event) {               // Prevents the default click action 				event.preventDefault();  				alert('Link disabled!'); 			}); 		}); 	</script> </body> </html> 

Next Article
JavaScript - Remove the href Attribute from Links Using jQuery or JS

A

anujpaz9pe
Improve
Article Tags :
  • JQuery
  • Web Technologies

Similar Reads

    How to disable HTML links using JavaScript / jQuery ?
    Given an HTML link and the task is to disable the link by using JavaScript/jQuery. Approach 1: Disable HTML link using JavaScript using setAttribute() Method. JavaScript setAttribute() Method: This method adds the defined attribute to an element and gives it to the passed value. In case of the speci
    5 min read
    How to remove underline from link using JavaScript?
    Given a link and the task is to remove the underline from the anchor element with the help of JavaScript. There are two approaches that are discussed below: Approach 1: Use textDecoration property of JavaScript to perform this operation. It can be set to many values but, in this example, it has been
    2 min read
    How to remove all attributes of an HTML element using jQuery ?
    In this article, we will see how to remove all attributes of an HTML element using jQuery. To remove all attributes of elements, we use the removeAttributeNode() method and .removeAllAttributes(). Syntax: $.fn.removeAllAttributes = function() { return this.each(function() { $.each(this.attributes, f
    1 min read
    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 remove “disabled” attribute from HTML input element using JavaScript ?
    In HTML, we use input elements for user input. The 'disabled' property helps prevent interaction with these elements. For example, if we only want users over 18 to input their Adhar card numbers, we can use JavaScript to remove the 'disabled' attribute when the user enters an age greater than 18. Th
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences