var input = document.createElement("input");
input.type = "text";
input.className = "css-class-name"; // set the CSS class
container.appendChild(input); // put it into the DOM
I think the following link will help you. If you want to generate fields dynamically and also want to remove them on the same time you can get the help from here. I had the same question, So i got the answer
function fun() {
/*Getting the number of text fields*/
var no = document.getElementById("idname").value;
/*Generating text fields dynamically in the same form itself*/
for(var i=0;i<no;i++) {
var textfield = document.createElement("input");
textfield.type = "text";
textfield.value = "";
document.getElementById('form').appendChild(textfield);
}
}
//Query some Dib region you Created
let container=document.querySelector("#CalculationInfo .row .t-Form-itemWrapper");
let input = document.createElement("input");
//create new Element for apex
input.setAttribute("type","text");
input.setAttribute("size","30");
containter.appendChild(input); // put it into the DOM
var input = document.createElement('input');
input.setAttribute('type', 'text');
document.getElementById('parent').appendChild(input);
Now the question is, how to render this process dynamic. As stated in the question, there is another input where the user insert the number of input to generate. This can be done as follows:
function renderInputs(el){
var n = el.value && parseInt(el.value, 10);
if(isNaN(n)){
return;
}
var input;
var parent = document.getElementById("parent");
cleanDiv(parent);
for(var i=0; i<n; i++){
input = document.createElement('input');
input.setAttribute('type', 'text');
parent.appendChild(input);
}
}
function cleanDiv(div){
div.innerHTML = '';
}
Insert number of input to generate: </br>
<input type="text" onchange="renderInputs(this)"/>
<div id="parent">
Generated inputs:
</div>
but usually adding just an input is not really usefull, it would be better to add a name to the input, so that it can be easily sent as a form.
This snippet add also a name:
function renderInputs(el){
var n = el.value;
var input, label;
var parent = document.getElementById("parent");
cleanDiv(parent);
el.value.split(',').forEach(function(name){
input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('name', name);
label = document.createElement('label');
label.setAttribute('for', name);
label.innerText = name;
parent.appendChild(label);
parent.appendChild(input);
parent.appendChild(document.createElement('br'));
});
}
function cleanDiv(div){
div.innerHTML = '';
}
Insert the names, separated by comma, of the inputs to generate: </br>
<input type="text" onchange="renderInputs(this)"/>
<br>
Generated inputs: </br>
<div id="parent">
</div>
<button id="add" onclick="add()">Add Element</button>
<div id="hostI"></div>
<template id="templateInput">
<input type="text">
</template>
<script>
function add() {
// Using Template, element is created
var templateInput = document.querySelector('#templateInput');
var clone = document.importNode(templateInput.content, true);
// The Element is added to document
var hostI = document.querySelector('#hostI');
hostI.appendChild(clone);
}
</script>
HTML Templates are now the recommended standards to generate dynamic content.
function generateInputs(form, input) {
x = input.value;
for (y = 0; x > y; y++) {
var element = document.createElement('input');
element.type = "text";
element.placeholder = "New Input";
form.appendChild(element);
}
}
input {
padding: 10px;
margin: 10px;
}
<div id="input-form">
<input type="number" placeholder="Desired number of inputs..." onchange="generateInputs(document.getElementById('input-form'), this)" required><br>
</div>
The code above, has a form with an input which accepts a number in it:
<form id="input-form">
<input type="number" placeholder="Desired number of inputs..." onchange="generateInputs(document.getElementById('input-form'), this)"><br>
</form>
The input runs a function onchange, meaning that when the user has entered a number and clicked submit, it run a function. The user is required to fill out the input with a value before submitting. This value must be numerical. Once submitted the parent form and the input are passed to the function:
The generate then loops according to the given value inside the input:
...
x = input.value;
for (y=0; x>y; y++) {
...
Then it generates an input inside the form, on each loop:
...
var element = document.createElement('input');
element.type = "text";
element.placeholder = "New Input";
form.appendChild(element);
...
I have also added in a few CSS stylings to make the inputs look nice, and some placeholders as well.
To read more about creating elements createElement():