1

Any clue how I can access each th under class="CenteredHeader" to add more parameters via jQuery to a tag?

<tr class="CenteredHeader">
  <th scope="col"></th>
  <th scope="col">
      <a href="/BoxOffice/Movies/807?sort=MovieName&amp;sortdir=ASC">Pelicula</a>
  </th>
  <th scope="col">
      <a href="/BoxOffice/Movies/807?sort=TechnologyName&amp;sortdir=DESC">Tecnologia</a>
  </th>
</tr>
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 2
    this question can help too: http://stackoverflow.com/questions/11369182/how-to-replace-elements-attr-href-with-each-strip-url – Barlas Apaydin Aug 14 '12 at 12:06
  • +1 good question. If I vote up on an answer it is only fair to assume it was a good question :) – Nope Aug 14 '12 at 12:10

3 Answers3

8
$('.CenteredHeader th a').each(function() {
    $(this).attr('href', function(i, oldhref) {
         return oldhref + '&amp;p1=111'
     });
})​;

You can use different parameter to each a tag by above solution if you want.

Can also do as @François Wahl comment stated:

$('.CenteredHeader th a').attr('href', function(i, oldhref) {
     return oldhref + '&amp;p1=111'
})​;
Community
  • 1
  • 1
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • what is the min version of jQuery that supports setting a attr value with a function? It looks cool :) – Liviu T. Aug 14 '12 at 12:02
  • 2
    @LiviuT. - Version 1.1 and up. (So _almost_ any version.) You can do the same thing with other methods like `.html()`, `.text()` and `.val()`. – nnnnnn Aug 14 '12 at 12:07
  • 1
    P.S. Note that you don't need the `.each()` loop if using that syntax of `.attr()`, your `.attr()` callback will be called for each element in the jQuery object... – nnnnnn Aug 14 '12 at 12:10
  • @thecodeparadox Thanks man! You read my mind! Awesome answer. It is exactly I need. – NoWar Aug 14 '12 at 12:13
  • @nnnnnn: I didn't want to edit the answer but you are correct. This works too: `$('.CenteredHeader th a').attr('href', function(i, oldhref) { return oldhref + '&p1=111' });`, see demo: http://jsfiddle.net/j7AGQ/. @thecodeparadox: feel free to add it to your answer if you like. – Nope Aug 14 '12 at 12:17
2

Try this:

$('.CenteredHeader th a').attr('href', "/BoxOffice/Movies/807?sort=MovieName&amp;sortdir=ASC&amp;p1=111")
Ram
  • 143,282
  • 16
  • 168
  • 197
  • 2
    +1 for single line solution. As far as I understood OP wants to change all the `href` attributes for a tags in each th. This seems to do it. – Nope Aug 14 '12 at 12:02
  • I'm not sure that this is appropriate given that the th in the example has two anchors with different href values. – nnnnnn Aug 14 '12 at 12:06
  • 1
    @nnnnnn: True, it wasn't quite obvious thogh from original post that each row is not going to be set to the same value. If only the end of the text has to change `thecodeparadox`'s answer might be more adabtable alright. – Nope Aug 14 '12 at 12:07
1
$('.CenteredHeader a').each(function(index, a) {
  var newHref = $(a).attr('href')+'&key=value';
  $(a).attr('href', newHref );
});
Liviu T.
  • 23,584
  • 10
  • 62
  • 58