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:

1
$("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:

1
$("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:

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

Recently Published

»

Modern Changes That You Should Consider for Your Web Site

Websites need to modernize and get updated periodically otherwise ...

»

Super Tips For A Flawless Website

A website has a specific purpose. And your job is to design a website ...

»

Wix Offers Best Free Web Apps for Artists and Photographers

The famous online website builder Wix is known for providing the ...

»

What You Should Know Before Setting Up a Digital Marketing and Web Design Company

Running a digital marketing and web designing company can be a ...

»

Important Factors to Think About When Starting a Mailing List

Many bloggers and business owners don’t have a mailing list, ...

»

Tried and Tested Strategies for Boosting eBook Sales

As you have by now realized, writing your ebook was just the first ...

»

What Really Makes a Website User-Friendly

Whether it’s for business or personal use, having a website online ...

»

5 Features of Virtual Private Server that put you in complete Control of Your Website

For a decade now, virtual private server has been a preferred hosting ...

»

8 Ways the Right Ecommerce Platform Makes Your Business Perform Better

Starting your own ecommerce store is an exciting experience, but ...