tags:

views:

21

answers:

2

Hi there,

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>
+1  A: 
function storeId(uID) {
    $('a.special').each(function(){
        link = $(this).attr('href');
        nlink = link + '&variable='+uID;
        $(this).attr('href', newlink);
    });
}

Give that a try.

inkedmn
you just unrolled his code >.<
Mark
not exactly - he has unescaped quotes inside the second .attr call.
inkedmn
+2  A: 

Are you sure that storeId isn't getting called twice? seems like it is.

seth