tags:

views:

182

answers:

3

I have a list of links that all go to a google maps api.

the links already have the daddr (destination) parameter in them as static. I am using Geo-Location to find the users position and I want to add the saddr (source address) to the links once I get the data.

so basically I will need to add something like &saddr=50.1234567,-50.03452 at the tail end of all the links pointing to google maps

All the links have a class called directions-link

and from this page I have figured out how to change them:

$("a.directions-link").attr("href", "http://www.google.com/");

However I only want to append my value to the end of the href without changing what the href already is.

How can I do that?

Thanks!!

+6  A: 
var _href = $("a.directions-link").attr("href");
$("a.directions-link").attr("href", _href + '&saddr=50.1234567,-50.03452');

To loop with each()

$("a.directions-link").each(function() {
   var _href = $(this).attr("href"); 
   $(this).attr("href", _href + '&saddr=50.1234567,-50.03452');
});
gmcalab
Thank you, will this work if there is more than 1 directions-link all with different hrefs? Thank you!
John Isaacks
If you are appending the same thing for each instance of `a.directions-link` then yes. Otherwise, if you are appending a different value to each href, you will need to use an `each()` to iterate and append the respective value.
gmcalab
John Isaacks
Check my updated answer.
gmcalab
Yep, the loop is what I needed, Thanks! :)
John Isaacks
A: 
$("a.directions-link").attr("href", $("a.directions-link").attr("href")+"...your additions...");
Diodeus
+1  A: 

jQuery 1.4 has a new feature for doing this, and it rules. I've forgotten what it's called, but you use it like this:

$("a.directions-link").attr("href", function(i, href) {
  return href + '?q=testing';
});

That loops over all the elements too, so no need for $.each

zaius