views:

43

answers:

2

I need to append an associate ID to the end of all links that go to a certain URL.

Here is what I have but I can't get it to work.

(this will be in WordPress)

<script type="text/javascript">
jQuery(document).ready(function() {
   jQuery("a[href*=doitbest]").click(function() {
     this.append("?memberid=5705&associate=true");
   });
 });
</script>

Any thoughts?

+1  A: 

First, I believe you have to use $(this) instead of this.

You also wish to change the URL of the link. The change should be made to href attribute, so

 $('a#my_link').click( function (event) {
  $(this).attr('href', $(this).attr('href') + '&id=1');
});
Extrakun
+2  A: 

Assuming that your selector (a[href*=doitbest]) works, you could try:

jQuery(document).ready(function() {
   jQuery("a[href*=doitbest]").click(function() {
      href = $(this).attr('href');
      del = href.indexOf('?') > -1 ? '&' : '?';
      href += del + 'memberid=5705&associate=true';
      $(this).attr('href', href);
   });
});

This changes the href attribute, not the link text. I'm not entirely sure that this is what you want, though. The del variable is used to append the URL part by means of the & or ? char.

jensgram
This didn't work, but I forgot to mention that the links I'm trying to append to are coming from an RSS feed that I'm displaying to the screen. Does this make a difference?
Adam Munns
@Adam Munns Could be - if the links are not part of the DOM when the click handler is attached, no `href`'s are getting manipulated. Please post more code or a link to a live demo. Also, try to put a simple `alert()` inside the `click` function in order to see whether the handler is attached properly.
jensgram
How can I tell if the parsed RSS feed is part of the DOM? The alert didn't appear to do anything.
Adam Munns
What code would you need to see? I'll try and get a live version up in the meantime
Adam Munns
@Adam Munns You can see the above in action here: http://jsfiddle.net/ga8qL/2/
jensgram
Hey everybody I decided to use PHP to modify the feed instead.
Adam Munns