1

I am trying to understand how can I make a list of x elements when a value change. Let's go with the example

HTML

<select class="num">
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 <option value="5">5</option>
</select>

<div class="list"></div>

jQuery

<script>
    $('.num').change(function() {
      num_val = $(this).val(); // get the number of the elements that will be shown
      for (i =0; i == num_val; i++) { // I do a for loop for append each div to the list but it doesn't work it's show me just 1.
         $('.list').append('<div>' + i + '</div>'); 
      }
    });
</script>

So, how can I display X number element equal to the value on select input?

1
  • for (i =0; i< num_val; i++) Commented Nov 25, 2013 at 12:12

3 Answers 3

1

Change you html into:

<select class="num">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

or the value will be always 1, and you'll add only one element.

Than cange you loop condition into:

 $('.num').change(function () {
     num_val = $(this).val(); // get the number of te elements that will be shown
     $('.list').empty();
     for (i = 0; i < num_val; i++) { // i do a for loop for append each div to the list but it doesn't work it's show me just 1.
         $('.list').append('<div>' + i + '</div>');
     }
 });

I used $('.list').empty(); to clear the list before append elements to it.

Demo: http://jsfiddle.net/IrvinDominin/Y79ec/

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

1 Comment

@Gilbert you are welcome! If solved consider to tick the answer as solved
1

DEMO

note the changed value

<option value="3">3</option>

$('.num').change(function() {  
      for (var i=0; i<this.value; i++) {
         $('.list').append('<div>' + (i+1) + '</div>'); 
      }
});

Performance-wise it's a bad idea to catch your .list element and create HTML elements all over again in a loop, so a better approach would be:

var list = '';

$('.num').change(function() {  
      for (var i=0; i<this.value; i++) {
         list += '<div>' + (i+1) + '</div>';
      }
      $('.list').append( list ); // only once after the loop.
});

Comments

0

try something like this

html code

<select class="num">
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option>
</select>

javascript code

    $(document).ready(function(){
         $('.num').change(function() {
             $('.list').html(''); 
              var num_val = parseInt($(this).val()); // get the number of te elements that will be shown
              console.log(num_val);
              for (var i =1; i <= num_val; i++) {
                 $('.list').append('<div>' + i + '</div>'); 
              }
        });
    });

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.