1

I am in the process of making an HTML form where users have different options. I am trying to make a button that infinitely generates a new set op input fields with increasements in the name, like: The first generated input should have a name of input1. The next with a name of input2 and so on.

Here is a visual example: https://webmshare.com/ZBvw0

How can this be accomplished?

1
  • Welcome to SO! What have you tried so far? Commented Aug 23, 2016 at 15:09

3 Answers 3

4

You can solve this problem by creating your form elements dynamically and appending them to your form element.

Below a simplified example, just to show the main idea.

Main points here are:

Document.createElement() - Which creates a specified HTML element (your form elements in this instance).

Node.appendChild() - Which adds a node to the end of the list of children of a specified parent node (your form element in this instance).

(function() {
  var counter = 0;
  var btn = document.getElementById('btn');
  var form = document.getElementById('form');
  var addInput = function() {
    counter++;
    var input = document.createElement("input");
    input.id = 'input-' + counter;
    input.type = 'text';
    input.name = 'name';
    input.placeholder = 'Input number ' + counter;
    form.appendChild(input);
  };
  btn.addEventListener('click', function() {
    addInput();
  }.bind(this));
})();
input{
  display: block;
}
<form id="form" action="">
</form>
<button id="btn" type="button">Click Me!</button>

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

5 Comments

I cant see what "var template" is supposed to do?
Is it still not really what I was looking for. As I said in the question, I need to create a set of inputs, not just one. An answer like the one Joshua provided would be perfect except the fact that it deletes the previous input data.
@SkinFlipNetwork in function addInput you can add any element as you wish by simply using document.createElement() (there you can create any type of DOM element)
As you can see here pastebin.com/BDUYG11p this is the block of code that needs to be duplicated each time. If I were to create an element for each item in there that would take forever. Isn't there an approach without createElement that works?
Hi. I got it working by altering the code a bit. Instead of using createElement to make an input, I had it make a div which I afterwards changed the innerHTML of so that it duplicated the exact lines of code that I need. Here's a pastebin: pastebin.com/ELQj8YTh
0

You can use jquery to fetch the name of the last item.

Then you can use the javascript string replace method and replace 'input' with '' in order to get the number of the last item.

Then just increment it by 1. You will have to parse it as an integer before adding 1 to it.

Then with the incremented number, create a new input field and append it to your container.

Comments

0

Try this

HTML

<div id="demo">
</div>
<input type="button" id="add" value="Add input"/>

Javascript

var num = 1;
document.getElementById('add').addEventListener("click",addInput);

function addInput(){
var demo = document.getElementById('demo');
demo.insertAdjacentHTML('beforeend','<div class="form-holder" style="width: 30%;"><a class="form-label">Billet type</a> <br><select name="ttype'+num+'"><option value="normal">Standard Billet</option><option value="add-on">Tilkøbs Billet</option></select></div><div class="form-holder" style="width: 31%; margin-left: 0.6%;"><a class="form-label">Billet navn</a> <br><input name="tname'+num+'" type="text" placeholder="F.eks. Entré Billet" style="width: 100%;" /></div><div class="form-holder" style="float: right; width: 18%; margin-left: 1%;"><a class="form-label">Antal</a> <br><input name="tquan'+num+'" type="text" placeholder="F.eks. 500" style="width: 100%;" /></div><div class="form-holder" style="float: right; width: 18%;"><a class="form-label">Pris (DKK)</a> <br><input name="tprice'+num+'" type="text" placeholder="F.eks. 100" style="width: 100%;" /></div> <br>');
 num++;
}

Check out jsFiddle example

6 Comments

There is an issue with this code. After adding a new input element all values entered before are destroyed.
Can that be fixed? Because other than that, this is exactly what I need
@SkinFlipNetwork with this code not really, to make it work you must retrieve the value of each input, store them in some place and repopulate innerHTML with HTML template included input.value. But to be honest. I see no point why you should go in this way and instead I would use a more simple approach as using document.createElement("input"); which is clear and does its job properly. Do you have any special requirements?
Yes. I need to create more than one input at a time. This is the exact set of inputs that I need replicated each time: pastebin.com/BDUYG11p
@SkinFlipNetwork i have made the necessary changes code no longer destroys values entered before
|

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.