jQuery

How to change the href for a hyperlink with jQuery

In this tutorial, we are going to see how to change the href for a hyperlink with jQuery. If you want to change the value of the href attribute using JQuery, you can use attr or prop functions depending on the version of JQuery being used.

If you are using JQuery 1.5 or lower, use the attr function:

$("a").attr("href", "https://stackhowto.com/");

If you are using JQuery 1.6 or higher, use the prop function:

$("a").prop("href", "https://stackhowto.com/");

The difference between prop and attr is that attr grabs the HTML attribute, while prop grabs the DOM property.
 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *