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

7 Answers 7

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';
}
Sign up to request clarification or add additional context in comments.

7 Comments

@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 : jsperf.com/each-vs-function
@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 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
@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 Probably something to do with this (source code)
|
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';
});

2 Comments

@Blazemonger Thanks and I'll accept Karl-André Gagnon's answer. You have wow ans and how to remove the added text "?a=text" with the another onChange event?
"return str.split('?')[0];" should do it.
6
$('#pager a').each(function() {
   $(this).attr('href', $(this).attr('href') + '?a=text');
});

Comments

5

Simply use the attr property. Example :-

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

This will do the job.

Comments

3
$("#pager").find("a").each(function(){
var $this=$(this);
$this.attr("href",$this.attr("href")+"?a=text");
})

Comments

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');

Comments

2

Try this code :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.