Classifying li in a div

Asked

Viewed 52 times

3

Folks I’m needing to alphabetically classify a div, use the following strategy, put in a div with display: none I have read them all, so after inserting all the html, I do the while for as long as you have li in that div it will check which is larger, the larger, identifying it adds in another div, that’s like display:block after I removed the div with display:none and stay in this loop.

My code is following:

while($('#check_presencaNone li').length > '0'){
  $('#check_presencaNone li').each(function(i){
     if($(this).text().toUpperCase() < valor){
       valor = $(this).text().toUpperCase();
       ponteiro = $(this);
     }
  });
  ponteiro.clone().appendTo('#check_presenca');
  ponteiro.remove();   
  console.log($('#check_presencaNone li').length);
}

While running is getting in an infinite loop, and I’m not able to identify the error. What’s wrong ?

  • 2

    first I would not use so '0' and yes without the quotes ... as the length is int .... use the 0 without quotes tbm int. something else ... in this case $('#check_presencaNone li'). length You need to go down it ne to get to the point where it zeroes .... the way this one will loop infinitely even

  • How decrease ? Because for me the lenght would solve, what would be the editions proposed by you ?

  • Because the idea is to remove it, remove the line that has already been ordered in the case. I need to actually sort the div.

  • understood master ... Voce could set an example to work so we can help ?

  • Xii, it’s hard because it’s something dynamic

  • But I’ll try.

  • I understand that it is difficult to make an example of this ... but we agree that without it is also complicated right, having the whole vision to try to find the problem

  • https://jsfiddle.net/a79vn0jx/ This is where I got it

Show 4 more comments

2 answers

1

The first point that would solve your question would be to change the while code to the bottom one:

for(i = 0; i <= $('#check_presencaNone li').length; i++) {
  $('#check_presencaNone li').each(function(i){
     if($(this).text().toUpperCase() < valor){
       valor = $(this).text().toUpperCase();
       ponteiro = $(this);
     }
  });
  ponteiro.clone().appendTo('#check_presenca');
  ponteiro.remove();   
  console.log($('#check_presencaNone li').length);
}
  • I took the opportunity to add the answer to the initial problem

0

In fact I had some questions of logic along with the response of our friend @Otto came to this resolution.

Just min, reset variable value valor, Because when he walked by for the first time and identified the smallest, he selected the pointer, so every time he walked the same pointer to populate the screen. then the code with the correct solution was like this.

var ponteiro = null,
valor = 'ZZZZZZZZ';
while( $('#check_presencaNone li').length > 0) {
 $('#check_presencaNone li').each(function(i){
   if($(this).text().toUpperCase() < valor){
      valor = $(this).text().toUpperCase();
      ponteiro = $(this);
   }
 });
 ponteiro.clone().appendTo('#check_presenca');
 ponteiro.remove();
 valor = 'ZZZZZZZZ';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.