Change the href using jQuery

If you need to change the href attribute of a link for some reason then applying this little bit of jQuery will do the trick. Just remember you might want to be quite specific with you selector when targeting the link, otherwise it will override others on your page.

The snippet below will modify the href of all hyperlinks to point to Web Design Weekly.

$(".your-class-name a").attr("href", "http://www.web-design-weekly/");

1 Comment

  1. Hello,

    thanks for post, I found it at google. Maybe do you know more convinient way to add attributes, instead of:

    $(‘a’).attr(‘href’, ‘http://google.com?param1=’ + someVar + param2 + anotherVar + …) et.c.?

This post currently has one response. What do you think?

You can use basic HTML when posting code, please turn all < characters into &lt; or > into &gt;
If the code is multi-line, use <pre><code></code></pre>

Leave a Reply

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