Reputation: 11240
I have a bunch of links on a page that on an action, I run a function and it appends an extra variable like so:
function storeId(uID) {
$('a.special').each(function () {
$(this).attr('href', $(this).attr('href') + '&variable=' + uID + '');
});
}
My understanding is, the above would go through each link identified, and grab the existing href, then append the variable bits onto the end. I have got the variable appending, but its not working as expected... e.g its appending the '&variable=' part twice, the first time, like '&variable=undefined' and then on top of that '&variable=23'.
Is there a better way to do this? Or a way I can just say go through each of these links and simply update the value that is set to the variable rather than rewriting the whole href?
So just to re-iterate, my original link is just something like this:
<a class="special" href="/page.php?random=1">link name</a>
And the aim is to have it look like the following after the function is performed:
<a class="special" href="/page.php?random=1&variable=32">link name</a>
But it winds up looking like:
<a class="special" href="/page.php?random=1&variable=undefined&variable=32">link name</a>
Upvotes: 0
Views: 67
Reputation: 37277
Are you sure that storeId isn't getting called twice? seems like it is.
Upvotes: 2
Reputation: 28215
function storeId(uID) {
$('a.special').each(function(){
link = $(this).attr('href');
nlink = link + '&variable='+uID;
$(this).attr('href', newlink);
});
}
Give that a try.
Upvotes: 1