How can I go through each link on a page and append something with jQuery?
<p>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:</p>

<pre><code>function storeId(uID) {

$('a.special').each(function(){

 $(this).attr('href',$(this).attr('href')+'&amp;variable='+uID+'');

});

}
</code></pre>

<p>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 '&amp;variable=' part twice, the first time, like '&amp;variable=undefined' and then on top of that '&amp;variable=23'. </p>

<p>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?</p>

<p>So just to re-iterate, my original link is just something like this:</p>

<pre><code>&lt;a class="special" href="/page.php?random=1"&gt;link name&lt;/a&gt;
</code></pre>

<p>And the aim is to have it look like the following after the function is performed:</p>

<pre><code>&lt;a class="special" href="/page.php?random=1&amp;variable=32"&gt;link name&lt;/a&gt;
</code></pre>

<p>But it winds up looking like:</p>

<pre><code>    &lt;a class="special" href="/page.php?random=1&amp;variable=undefined&amp;variable=32"&gt;link name&lt;/a&gt;
</code></pre>
