j1nma
j1nma

Reputation: 467

Add desired number of inputs text fields

I just started coding and I dont know much. My wish is to have a button that when clicked an option drop down menu appears with numbers (1,2,3,4) to be selected. So when a number is chosen, for example, 2, then two text fields would be added after the last text field. This is what I have now:

<script type="text/javascript">
    $(function(){
        var counter = 0;
        $('#addinput').click(function(){
            $('<input class="search" style="margin-bottom:4px; display:none;" type="search" autofocus="autofocus" name="word' + counter++ + '"/>').appendTo('#inputs').fadeIn(400);
        });

        $('#button').click(function(){
            $('.search').each(function(){
                var $this = $(this);
                if(!$this.val()){
                     $this.remove()
                }
            });    
        });
    }) 
</script>

Thank you all!

Upvotes: 0

Views: 72

Answers (1)

kasper Taeymans
kasper Taeymans

Reputation: 7026

made a jsfiddle to help you getting started.

http://jsfiddle.net/kasperfish/89jGS/2/

<select id="number" >
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>
<span id="textfields"></span>



$(function() {

  $('#number').change(function(){
      $('#textfields').html('');
      val= $(this).val();     
      for (i=0;i<val;i++){
          $('#textfields').append('<input type="text" name="'+i+'">');
      }      

  });  


});

Upvotes: 3

Related Questions