How to change the href for a hyperlink using jQuery

To change the href for a hyperlink using jQuery you can use the following snippet.
The following example will change every href to http://codesnippets.fesslersoft.de.

Sample jQuery

$("a").attr("href", "http://codesnippets.fesslersoft.de/")

if you want to change every href pointing to http://codesnippets.fesslersoft.de with a href pointing to http://codesnippets.fesslersoft.de/change-the-href-for-a-hyperlink you can use the following snippet.

Sample jQuery

$("a[href^='http://codesnippets.fesslersoft.de/']")
   .each(function()
   { 
      this.href = this.href.replace("http://codesnippets.fesslersoft.de/", "http://codesnippets.fesslersoft.de/change-the-href-for-a-hyperlink");
   });

2 thoughts on “How to change the href for a hyperlink using jQuery

Leave a Reply