How to Change the Href for a Hyperlink using jQuery

Hyperlinks, also known as “links”, are a crucial aspect of any website.

They allow users to navigate from one page to another, either within the same website or to another website altogether.

Hyperlinks are created using the HTML “a” tag, with the “href” attribute determining the URL that the link will redirect to.

In some cases, it may be necessary to change the href of a hyperlink dynamically using jQuery.

This is particularly useful in single-page applications where the content of a page changes without reloading the whole page.

In this Javascript tutorial, we’ll go over how to modify the href of a hyperlink using jQuery.

We’ll provide code examples along the way to help illustrate the concepts being discussed.


jQuery Selectors

To modify the href of a hyperlink using jQuery, you first need to select the element that you want to modify.

jQuery provides a number of selectors that can be used to select elements based on their id, class, type, or other attributes.

For this example, we’ll use the “id” selector to select a hyperlink with a specific id.

Here’s an example of a hyperlink with an id of “myLink”:

<a id="myLink" href="https://www.example.com">Example Link</a>

To select this element using jQuery, we can use the following code:

var link = $('#myLink');

jQuery .attr() Method

Once you have selected the element that you want to modify, you can use the jQuery .attr() method to modify its attributes.

The .attr() method takes two arguments: the first argument is the name of the attribute you want to modify, and the second argument is the new value that you want to set for the attribute.

To modify the href of the hyperlink we selected in the previous section, we can use the following code:

link.attr('href', 'https://www.newexample.com');

The code above sets the href of the hyperlink with an id of “myLink” to “https://www.newexample.com“.

Putting it All Together

Here’s the complete code for changing the href of a hyperlink using jQuery:

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script>
  $(document).ready(function(){
    var link = $('#myLink');
    link.attr('href', 'https://www.newexample.com');
  });
  </script>
</head>
<body>

<a id="myLink" href="https://www.example.com">Example Link</a>

</body>
</html>

In this example, we first include the jQuery library using the “script” tag.

We then use the $(document).ready() method to ensure that the code is executed only after the document has finished loading.

Within the $(document).ready() method, we select the hyperlink with an id of “myLink” and modify its href using the .attr() method.


Conclusion

In this tutorial, we covered how to modify the href of a hyperlink using jQuery.

We discussed the basics of jQuery selectors and the .attr() method, and provided code examples to illustrate the concepts.

With this knowledge, you should now be able to modify the href of a hyperlink dynamically using jQuery in your own web projects.

It’s worth noting that there are other ways to change the href of a hyperlink using jQuery, such as using the .prop() method, but the .attr() method is the most straightforward and commonly used method for this purpose.

In conclusion, changing the href of a hyperlink using jQuery can be a useful technique for creating dynamic and interactive websites.

Whether you’re a tech blogger, programmer, software developer, or technical writer, understanding how to modify hyperlinks in this way is a valuable skill to have.