How to change the href Attribute value for a Hyperlink using jQuery

When coding with jQuery, at some point or the other you would want to modify the source of a particular hyperlink using jQuery. In this micro post we will help you with some simple code snippets on how you can change the href attribute of a given by applying just a tricky jQuery logic.

The only thing that you need to be careful about with this is that you will have to be particular with you selector when you are targeting a link otherwise the possibility is that your other web pages will be overridden.

For instance, let us suppose that there is a hyperlink that points to orkut.com and now your project requires you to change the hyperlink to facebook.com then you can do this by employing the simple code snippet shown below:

$("a [href="http://www.orkut.com/"]").attr ("href", "http://www.facebook.com/")

However if you would like to change all the hyperlinks on your webpage to point to a custom link say yahoo then the below code snippet will help you modify this:

$("a”).attr ("href", "http://www.yahoo.com/")

For jQuery version above 1.6, you can use prop () instead of attr () to change the href of a hyperlink, so the above code for higher versions can be replaced as:

$("a”).prop ("href", "http://www.yahoo.com/")

Recently Published

»

Is Your Business Site Fully Accessible?

Does your business site have full accessibility? Even with the ADA ...

»

Secure Web: 5 Best Practices For Application Development

There are several essential best practices for secure web ...

»

Laravel PHP Framework: Features and benefits for web development

Companies and organizations are focusing on developing custom web ...

»

Web Development Vs. Software Development: What Is the Difference?

As technology continues to transform all aspects of our lives, a lot ...

»

Web Redesign : Is Your Landing Page Begging for one?

How do you know when it’s time to web redesign your landing page? ...

»

Customer Acquisition: Few Steps To Improve This.

Customer acquisition is essential to any business’s success — to ...

»

Mobile App Development Tools: 5 Enterprise-Ready For Rapid Deployment

There are several enterprise-ready mobile app development tools for ...

»

Docker: How To Build Better Development Security With It

The top concern among developers and DevOps teams is security within ...

»

Mobile Development: How to Reduce Costs

Modern markets have become so competitive that it has become vital ...