abhijit
abhijit

Reputation: 1968

each in jquery binds element in order(top to bottom)?

i am facing a problem when am trying to bind a array with the ids of my div's i wanted to bind them in order of their appearance staring with first to last.. The code:

i=1;
$('.hiddenvalue').each(function(){
   arrayinit[i] = $(this).attr("id");
   i++;
});

but its binding in any random order, why is it so? any suggestions?

Upvotes: 1

Views: 324

Answers (3)

graphicdivine
graphicdivine

Reputation: 11211

"i" seems redundant. Try...

$('.hiddenvalue').each(function(){
   arrayinit.push( $(this).attr("id") );
});

Upvotes: 3

malko
malko

Reputation: 2382

does this give you the same results ?

var arrayinit=[];
$('.hiddenvalue').each(function(){
   arrayinit.push($(this).attr("id"));
});

I'm not sure it'll change the order, i think the order is more the same as it appear in the DOM tree does your .hiddenvalue divs appear in the correct order in the DOM tree ?

Upvotes: 2

genesis
genesis

Reputation: 50976

try this one instead

$('.hiddenvalue').each(function(i){
   i++;
   arrayinit[i] = $(this).attr("id");
});

However, I'm not quite sure why is it happening.

Upvotes: 1

Related Questions