Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
How to create a Home icon using jQuery Mobile ?
Next article icon

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 Content

  • Using the attr() method
  • Using the prop() method

Using the attr() method

The 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() method

The 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:


Next Article
How to create a Home icon using jQuery Mobile ?

B

blalverma92
Improve
Article Tags :
  • Web Technologies
  • HTML
  • JQuery
  • 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 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 change the color of an icon using jQuery ?
    In this article, we will see how to change the color of the icon using jQuery. To change the color of the icon, we will use a jquery method. jQuery css() method this method is used to change the styling of a particular selector. This method is also can be used for changing the color of the icon. Fir
    2 min read
  • How to create a Home icon using jQuery Mobile ?
    jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a Home icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" hr
    1 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>
    1 min read
  • How to change icon based on zoom level using jQuery ?
    In this article, we will see how to change icon based on zoom level using jQuery, along with understanding its basic implementation through the examples. CSS zoom property in jQuery allows us to set the zoom value for a specific element and also allows us to get and set the value of these properties
    4 min read
  • How to change the background image using jQuery ?
    To change the background image using jQuery, you can use the jQuery CSS() method. For this, the whole property value is specified using the url() functional notation. Approach: Suppose we have an image URL stored in a variable and then use css() method to change the value of background image. Below
    1 min read
  • How to make a call-able link using HTML ?
    To make a callable link using HTML means creating a hyperlink that allows users to initiate a phone call directly from their devices. This is done using the <a> tag with the href attribute set to tel: followed by the phone number. When clicked, this link opens the phone dialer on supported dev
    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 Content Using JavaScript DOM methodsUsing jQuery prop() MethodUs
    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