I need to update href value thorughout the page using jquery. Say href="http://www.google.com?gsec=account" should be changed to href="http://account.google.com?gsec=account" how i can get this done.
-
I assume you want a dynamic solution that searches the page, and does a replace instead of assigning a predetermined `href` to one predetermined `` element. If so, I added an answer below that accomplishes that. – user113716 Aug 11 '10 at 14:22
-
Hmm... I posted one assuming that "account" was a dynamic value, and that there may be other variations to the URL (e.g., http/https and other query string variables). I might have gone overboard after seeing your response though lol. – jmar777 Aug 11 '10 at 14:27
-
@jmar - Yeah, my answer does have a few requirements about what is replaced. A little tweak in the selector and in the `replace()` using a regex would open it up a little more. – user113716 Aug 11 '10 at 14:35
7 Answers
This will do the replace throughout the page that I think you're looking for.
// Find `<a>` elements that contain `www.google.com` in the `href`.
$('a[href*="www.google.com"]').attr('href', function(i,href) {
// return a version of the href that replaces "www." with "accounts."
return href.replace('www.', 'accounts.');
});
Try it out: http://jsfiddle.net/dT8j6/
EDIT: This version allows for https://
and for links without the www.
.
Try it out: http://jsfiddle.net/dT8j6/1/
$('a[href*="google.com"]').attr('href', function(i,href) {
return href.replace(/^http(s*):\/\/(www\.)*google.com/, 'http$1://accounts.google.com');
});
EDIT: If you only wanted to change elements that have gsec=account
, then change the selector to $('a[href*="gsec=account"]')
.

- 318,772
- 63
- 451
- 440
-
Won't this modify links to google that don't have the "gsec=account" param? – jmar777 Aug 11 '10 at 14:44
-
@jmar777 - Yes. I wasn't certain if that specific param was the requirement. Changing the selector to `$('a[href*="gsec=account"]')` would focus only on those elements. – user113716 Aug 11 '10 at 14:47
-
Ya... which is probably 98% safe, but of course fails for yahoo.com?gsec=account, or gsec=accountfoo, etc. Not sure how _exact_ he needs this thing to be. Question is somewhat vague. – jmar777 Aug 11 '10 at 14:52
-
@jmar777 - Yes, I'm assuming OP controls the content of the href attributes. The selector can be as specific as OP needs. Could use `$('a[href$="google.com?gsec=account"]')` which adds `google.com` and uses the `ends-with` selector instead of `contains`. There will always be a way to break it if OP doesn't give specific requirements. :o) – user113716 Aug 11 '10 at 14:58
lets say you have this:
<a href="http://www.ibm.com" id="myLink">
you should use this:
var newHref = "http://google.com";
$("#myLink").attr('href', newHref );

- 188,729
- 360
- 878
- 1,366
This is a duplicate of:
How to change the href for a hyperlink using jQuery
One scenario it doesn't mention is if you set an id on your anchor that needs to be changed then you can use the id as a selector in jQuery.
$("#LinkId").attr('href', "http://account.google.com?gsec=account")

- 1
- 1

- 6,318
- 23
- 21
This should hopefully provide a pretty full solution to your problem (as best I can interpret it, anyway):
$(function() {
$('a').each(function() {
var $this = $(this),
href = $this.attr('href');
var res = href.match(/(.*?)(www)(\.google\.com.*?)([?&]gsec=)([^&]+)(.*)/);
if (null != res) {
// remove the "full match" entry
res = res.slice(1);
// replace www match with account match
res[1] = res[4];
// update the href attribute
$this.attr('href', res.join(''))
}
});
});
edit: If "account" is a static value, then this will work as well:
$(function() {
$('a').each(function() {
var $this = $(this),
href = $this.attr('href');
var res = href.match(/(.*?\/\/)(www)(\.google\.com.*?)([?&]gsec=account)(&?.*)/);
if (null != res) {
// remove the "full match" entry
res = res.slice(1);
// replace www match with account match
res[1] = 'account';
// update the href attribute
$this.attr('href', res.join(''))
}
});
});
Please note that these solutions assume that there may be other variations in the URL, such as http/https and other query string variables.

- 38,796
- 11
- 66
- 64
I think you left something out in your question. Nevertheless:
var link = $("a"); // Or some other selector to access the link
link.attr("href", "http://account.google.com?gsec=account");

- 37,635
- 12
- 69
- 105
Use jQuery’s .attr()
method. With one parameter, it returns the attribute value, with two parameters you set it.
$("a#whatever").attr("href", "http://account.google.com?gsec=account");

- 7,132
- 2
- 27
- 35
If you are changing all the links then:
$(document).ready(function() {
$("a").attr("href", "http://account.google.com?gsec=account");
});

- 1,113
- 2
- 11
- 19