How to update (add to) href in jquery?

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

links already have the daddr (destination) parameter in them as static. I use Geo-Location to find the position of the user, and I want to add saddr (source address) to the links after receiving the data.

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

All links have a class called directions-link

and this page I figured out how to change them:

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

However, I only want to add a value to the end of href without changing what href already is.

How can i do this?

+58
jquery
May 10 '10 at 19:20
source share
4 answers
 var _href = $("a.directions-link").attr("href"); $("a.directions-link").attr("href", _href + '&saddr=50.1234567,-50.03452'); 

In a 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'); }); 
+112
May 10, '10 at 19:22
source share

jQuery 1.4 has a new feature for this, and it manages. I forgot what he called, but you use it like this:

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

It also covers all elements, so there is no need for $ .each

+31
May 10 '10 at 19:46
source share
 $("a.directions-link").attr("href", $("a.directions-link").attr("href")+"...your additions..."); 
+3
May 10 '10 at 19:22
source share

Here is what I was trying to do to add a parameter to the url that contains a specific character in the url.

 jQuery('a[href*="google.com"]').attr('href', function(i,href) { //jquery date addition var requiredDate = new Date(); var numberOfDaysToAdd = 60; requiredDate.setDate(requiredDate.getDate() + numberOfDaysToAdd); //var convertedDate = requiredDate.format('dM-Y'); //var newDate = datepicker.formatDate('yy/mm/dd', requiredDate ); //console.log(requiredDate); var month = requiredDate.getMonth()+1; var day = requiredDate.getDate(); var output = requiredDate.getFullYear() + '/' + ((''+month).length<2 ? '0' : '') + month + '/' + ((''+day).length<2 ? '0' : '') + day; // 

Working example Click

0
Nov 09 '15 at 8:49
source share



All Articles