tags:

views:

130

answers:

2

Hello,

I want to increment the value of i . The "for" loop does not work.

$("a[href$='.xls']").appendTo(".xl1").attr('id','xl'+i);

I search all excel files and places them in a container and increment the value of their id.

Thanks Jean

+2  A: 
$("a[href$='.xls']").each(function(i) {
  $(this).appendTo(".xl1").attr('id','xl'+i);
})

counting starts from i=0

You can read about function each() here http://api.jquery.com/each/

Kamil Szot
No need to define ´i´ separately: the first argument in the each() callback is the index
Álvaro G. Vicario
+1 - `each` is probably better here, since the OP is making several actions on every element.
Kobi
@Álvaro - you are right, your way is much cleaner and simpler. I edited the answer to take this into account.
Kamil Szot
A: 

Using jQuery 1.4.2:

var i=1;
$("a[href$='.xls']")
   .appendTo(".xl1")
   .attr('id', function(){return 'xl' + i++});
Kobi
How do you end with two id="xl1" elements in your code?
Jean
You don't, my mistake. You have a *class* of `xl1`. This is ok, but confusing...
Kobi