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 Answers2

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.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
  • 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 ..... –  Dec 21 '09 at 20:55
  • 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. – Paul D. Waite Dec 21 '09 at 21:14
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.

Drew Wills
  • 8,408
  • 4
  • 29
  • 40
  • 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) –  Dec 21 '09 at 20:31
  • 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? – Paul D. Waite Dec 21 '09 at 20:38