How to change href attribute of a hyperlink using jQuery ?
In this article, we will see how to change the href attribute of hyperlinks using jQuery. There are two ways or methods available in jQuery that we can use to change the URL of the href attribute.
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.
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.
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.
Example: The below example will explain the use of the prop() method to change the URL of the href attibute.
Share your thoughts in the comments
Please Login to comment...