Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update (append to) an href in jquery?

Tags:

jquery

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?

like image 521
JD Isaacks Avatar asked May 10 '10 19:05

JD Isaacks


People also ask

How to change href of a in jQuery?

Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.


1 Answers

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 $this = $(this);           var _href = $this.attr("href");     $this.attr("href", _href + '&saddr=50.1234567,-50.03452'); }); 
like image 183
Gabe Avatar answered Oct 01 '22 08:10

Gabe