0

I want to change the param of link dynamically.

For e.g.

  1. Link1
  2. Link2
  3. Link3

by default their url is ?item=text i.e. for link1(href="?item=link1") etc.. but when i click link1 the url of link2 and link3 should be link2(?item=link2&item=link1) link3(?item=link3&item=link1)

any idea how to acheive this?

Thanks,

2
  • See stackoverflow.com/questions/179713/… Commented Dec 21, 2009 at 20:19
  • 1
    yah I saw that post but my problem is different. Commented Dec 21, 2009 at 20:52

2 Answers 2

1

Assuming all the links have a class of superspeciallink, this should work:

$('a.superspeciallink').bind('click', function(){
    var querystring = this.search; // The search property of links gives you the querystring section of their href
    var originalhref = this.href;

    $('a.superspeciallink').each(function(){
        if(this.href != originalhref) {
            this.href = this.href + '&' + querystring.slice(1);
        }
    });

    return false;
});

This would mean that these links never get followed though — I assume some other JavaScript would be reading out these query string values eventually.

Sign up to request clarification or add additional context in comments.

2 Comments

this seems to be a nice approach I need to check this but in my case all the links are of same class. And actually its the method to filter the things .Yah you are rite its adding just query string one on to another but this is adding filters .....
Ah, okay. I’ve amended it to work with links which are all the same class. It doesn’t currently check whether the query string has already been added, so you could end up with ?item=link1&item=link3&item=link3&item=link3&item=link3&item=link3.
0

Invoke jQuery something like the following:

$("my#links").attr("href", "new/href/value");

You'll need to write a function to calculate the new value of href for each link, of course.

2 Comments

but it is dynamic for e.g. if i click on link3 so it should be same like if i click link3 first then link1 would be having url(?item=link1&item=link3). And if I click link2 after it url would be (link1:?item=link1&item=link2&item=link3)
If it’s meant to work how you describe, then none of these links will ever do anything other than add their query strings to each other. None of them will actually get followed. Should they only do the query string manipulation on the first go, or something?