0

I playing around with dropdown lists, and I am wondering on how I would dynamically add input box based on a number selected from a dropdown list. e.g. if one is selected then add one input box, if two then add two input boxes etc... . Any tips or guidance would be appreciated Thanks

1
  • Grab a tutorial on DOM manipulation. Commented Nov 12, 2012 at 14:50

1 Answer 1

2

I can show you a simple method using jquery:

html

<select id="dropdown">
  <option value="0">Select number of inputs</option>
  <option value="1">1 input</option>
  <option value="2">2 inputs</option>
  <option value="3">3 inputs</option>
  <option value="4">4 inputs</option>
</select>
<div id="input-holder"></div>​

jquery

$('#dropdown').change(function(){
    if ($(this).val() > 0) {
      $('#input-holder').empty();
      for (i = 1; i <= $(this).val(); i++) {
        $('#input-holder').append('<input type="text" name="input'+i+'" value="' + i +'" >');
      }
    }
});​

Of course this is just an example and it can be done in multiple other ways.

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

1 Comment

you can add it between <script> tags and don't forget to load a jquery library before using the code. For more info visit http://jquery.com/

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.