3

Hi i have this javascript .

<html>
    <div>
        <li class="class0 class1" data-slide-to="0">Start</li>
     </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
     var i;
         for(i=0; i<3; i++){
             $(".class1").after("<li Class='class0' data-slide-to='"+i+"'>"+i+"</li>");
         }
      </script>  
</html>

And i get the out put as

Start
3
2
1
0

But i want to get this result in following order

start
0
1
2
3

how i get this ?How to revert the result?

4
  • That loop will output 0, 1, 2 Commented Jan 6, 2017 at 8:30
  • Select last element of .class0 using$(".class0:last").append('target html') Commented Jan 6, 2017 at 8:32
  • 2
    to answer the question as specified "change for loop" ... for(i=3; i>=0; i--){ Commented Jan 6, 2017 at 8:32
  • the use of the word "output" suggests you were talking about the "output" - nevermind, deleted my miscorrection Commented Jan 6, 2017 at 8:36

3 Answers 3

2

Since you are using after() method it would append after the li. So get the parent and append the element. Although you need to update the condition in order to generate 4 elements and your HTML is not valid the li should be the child of ul or ol.

<html>
<div>
  <ul>
    <li class="class0 class1" data-slide-to="0">Start</li>
  </ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
  var i;
  for (i = 0; i < 4; i++) {
    $(".class1").parent().append("<li Class='class0' data-slide-to='" + i + "'>" + i + "</li>");
  }
</script>

</html>


Or geneate elements in reverse order.

<html>
<div>
  <ul>
    <li class="class0 class1" data-slide-to="0">Start</li>
  </ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
  var i = 4;
  while (i--) {
    $(".class1").parent().append("<li Class='class0' data-slide-to='" + i + "'>" + i + "</li>");
  }
</script>

</html>

Sign up to request clarification or add additional context in comments.

3 Comments

<= 3 to get required output :p (or < 4 as you say :p )
could you please explain more about parent() .
@John : the parent() method would get the parent of the element... in my snippet it would select ul..... and append() method add content at the end of ul...
2

Change your script to work the other way around. So start at 3, and stop when you're at 0:

 <script>
     var i;
     for(i=3; i>0; i--){
         $(".class1").after("<li Class='class0' data-slide-to='"+i+"'>"+i+"</li>");
     }
  </script>  

Comments

0
<html>
<div>
    <li class="class0 class1" data-slide-to="0">Start</li>
 </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
 var i;
     for(i=3; i>=3; i--){
         $(".class1").after("<li Class='class0' data-slide-to='"+i+"'>"+i+"</li>");
     }
  </script>  

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.