0

I have a dropdown box that allows users to select how many inputs they have. So for example perhaps they want to enter one word, they'll select one. If they want to enter two words, they'll select two.

Depending on which option they choose, I want to create that many textboxes below that I can also refer to using other scripts (like maybe through indexing).

Is there a way to do this?

1
  • Yes, there is. But, you need to use JavaScript. Are you familiar with it? Commented Dec 9, 2010 at 15:37

2 Answers 2

1

Sure, use Javascript to manipulate the DOM. In it's simplest form you could do something like:

document.getElementById('someDiv').innerHTML += "<label>Label:</label><input type='file' value='' />";

Or using something like JQuery:

var new_field = " <label>SomeLabel: </label> <input type = "text" / >";
$("#sonmeSelector").before(new_field);
Sign up to request clarification or add additional context in comments.

1 Comment

The javascript worked great. I just needed to know about innerHTML.
1

do you want some thing like this (done with jQuery 1.4.4):

HTML

<html>
  <head>
    <title>Multiple Input</title>
  </head>
  <body>
    <select id="amount" name="amount" onchange="addInput();">
        <option value="1" >1</option>
        <option value="2" >2</option>
        <option value="3" >3</option>
    </select>
    <div id="inputs">
    </div>
  </body>
</html>

JS

function addInput(){
  var amount = $('#amount').val();
  var inputs = $('#inputs').empty();
  for(i = 0; i < amount; i++) {
    inputs.append('<input type="text" name="input[' + i + ']" /> ');
  }
}

Also look at my jsfiddle http://jsfiddle.net/8KqUt/

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.