2

I want to make input type text refer to user input, if input 3 it shows 3 new input type text

I try this

<input type='text' id='how_many'><input type='button' id='add' value='add'>
<script>
       function add(){

          var total=$('#how_many).val();

          for(var x=0;x<=total;x++){
             //HOW TO ADD INPUT TYPE TEXT BELOW 
          }

       }
</script>
1
  • 2
    missing ' change to var total=$('#how_many').val(); Commented Jan 28, 2014 at 15:34

4 Answers 4

2

Probably you are missing '

var total=parseInt($('#how_many').val());


for(var x=0;x<=total;x++){
 $("#mainContainer").append("<input type='text'/>");
}
Sign up to request clarification or add additional context in comments.

1 Comment

but i want if #add clicked twice it prevent loop / destroy loop
1

There you go:

<div class="container">
    <input type='text' id='how_many' />
</div>

<input type='button' id='add' value='add' />

<script type="text/javascript">
       $(document).ready(function() {
          $("#add").click(function(e) {
               var $el = $('#how_many'),
              total = $el.val();

              for (var x=0;x<=total;x++){
                  $(".container").append($el.val("").clone().attr("id", "total_" + x));
              }
          });
       });
</script>

Where '.clone()' is a jQuery method which clones the DOM node, '.append()' is a jQuery method to append the newly cloned node to another node.

3 Comments

And what triggers the add function?
how to add id in the clone of input type text based from loop for, like id='total_1',id='total_2'
Wouldn't it make more sense to more the onlick to the add button instead of the text input? Better yet, just use jQuery to bind it instead of the inline bind?
0

Remember to parse your input, then just create:

var total= parseInt($('#how_many').val());

for(var x=0;x<=total;x++){
     $("yourcontainer").append("<input type='text' />");
}

Trigger the add() function on a blur

$("#how_many").blur(add);

Comments

0
$('#add').click(function () {
    for (i=0;i<$('#how_many').val();i++) {
        $(this).after('<div><input type="text" /></div>')
    }
})

jsFiddle example

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.