How to Add a quertystring to a link using jquery I have a set of links on my page and some of them start with /mypath, like this &lt;a href="/mypath/other/stuff"&gt;

I need to add a querystring to the links that start this way, so the result is something like &lt;a href="/mypath/other/stuff?parent=1234"&gt; with the '1234' being the node id of the node we're currently viewing.

I've sucessfully passed the nid into my js file using drupal settings.  The only part of this I can't get to work is putting the original link in where you see the second attr('href').

My code is as follows:

    $('a[href^="/mypath/"]').attr('href', attr('href') + '?parent=' + Drupal.settings.nodeinfo.nid);


Instead of that second attr('href') I've tried a number of things and can't figure out what it should be.
You've got nothing to hook onto for that second attr() as you're not storing the result of the jQuery call in a variable.

I'd advise using a loop instead:

$('a[href^="/mypath/"]').each(function(index, element) {
  var newAttr = $(element).attr('href') + '?parent=' + Drupal.settings.nodeinfo.nid;
  $(element).attr('href', newAttr);
});


That will catch any link on the page that matches the path selector.