17

I've the elements as follows,

<div id="pager">
<a href="/somepath/1">First</a>
<a href="/somepath/1">Previous</a>
<a class="" href="/somepath/1">1</a>
<a class="Current" href="/somepath/2">2</a>
<a class="" href="/somepath/3">3</a>
<a href="/somepath/3">Next</a>
<a href="/somepath/20">Last</a>
</div>

and I want it to be changed as follows within browser.

<div id="pager">
<a href="/somepath/1?a=text">First</a>
<a href="/somepath/1?a=text">Previous</a>
<a class="" href="/somepath/1?a=text">1</a>
<a class="Current" href="/somepath/2?a=text">2</a>
<a class="" href="/somepath/3?a=text">3</a>
<a href="/somepath/3?a=text">Next</a>
<a href="/somepath/20?a=text">Last</a>
</div>

So that I can use the "a" data values to next page. Can any one give me the code, which does the appends inside

div id="pager" -> <a> -> href="

and i wants to remove the added text with another onChange event.

Thanks in advance

user9371102
  • 1,278
  • 3
  • 21
  • 43

7 Answers7

34

Since jquery is tagged :

$('#pager a').each(function(){
     this.href += '?a=text';
})

Vanilla JS would look like this :

var a = document.getElementById('pager').getElementsByTagName('a'),
    length = a.length;

for(var i=0; i< length; i++){
    a[i].href += '?a=text';
}
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
  • @Bondye What is wrong with `.each` function? *"+1 Finally one without that .each() function... People don't understand .each().."*. The answer where you posted this comment, what do you think happen behind the code? Alsoo, `.each()` is faster : http://jsperf.com/each-vs-function – Karl-André Gagnon Jul 19 '13 at 13:48
  • @Karl-AndréGagnon Personally, I prefer to use `.attr()` with a function instead of `.each()` because I find it more readable. But I certainly can't argue with performance testing. (I wonder if some optimization in jQuery could fix that?) – Blazemonger Jul 19 '13 at 14:04
  • 2
    @Blazemonger Well that's a personnal preference since i prefer using `.each()`, So we cant arg on that :) @Bondye It is not looping twice, it is actually the same result as using a function (like Blaze's answer). Here the revelant part in jquery code : `for ( ; i < length; i++ ) { fn( elems[i], key, raw ? value : value.call( elems[i], i, fn( elems[i], key ) ) ); }. The only adding to the `.each` way is an additionnal function call (wich is why each is faster). I think your comment is more revelant for function like `.addClass()` wich is useless to loop through elements & call it after – Karl-André Gagnon Jul 19 '13 at 14:09
  • @Blazemonger on a side note, the vanilla js is the slowest one. Any idea why (i probably did something wrong in the code)? – Karl-André Gagnon Jul 19 '13 at 14:13
  • @Karl-AndréGagnon Probably something to do with [this (source code)](https://github.com/jquery/jquery/blob/master/src/core.js#L566) – Blazemonger Jul 19 '13 at 16:14
  • @Karl-AndréGagnon, how to remove the added text "?a=text" with the another onChange event? – user9371102 Jul 22 '13 at 06:21
  • @Vicky you can try `this.href = this.href.replace('?a=text', '');` – Karl-André Gagnon Jul 22 '13 at 12:34
15

.attr(), like many jQuery functions, can take a function argument that modifies the existing value:

$('#pager a').attr('href',function(i,str) {
   return str + '?a=text';
});
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
6
$('#pager a').each(function() {
   $(this).attr('href', $(this).attr('href') + '?a=text');
});
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
5

Simply use the attr property. Example :-

$("a").attr("href", "http://www.google.com/")

This will do the job.

Aamir Shah
  • 4,473
  • 8
  • 21
  • 28
3
$("#pager").find("a").each(function(){
var $this=$(this);
$this.attr("href",$this.attr("href")+"?a=text");
})
vikrant singh
  • 2,091
  • 1
  • 12
  • 16
2

you can use attr method

$('a').attr('href', '/somepath/1?a=text');

And if you want to change the href of a specific <a> give that '' a unique id and than change its href as follows

$('#link').attr('href', '/somepath/1?a=text');
Khawer Zeshan
  • 9,470
  • 6
  • 40
  • 63
2

Try this code :

$('#pager a').each(function(){
    this.href += '?a=text'
})
Lucas Willems
  • 6,673
  • 4
  • 28
  • 45