[javascript] How to create <input type=“text”/> dynamically

To create number of input fields given by the user


  1. Get the number of text fields from the user and assign it to a variable.

    var no = document.getElementById("idname").value


  1. To create input fields, use createElement method and specify element name i.e. "input" as parameter like below and assign it to a variable.

    var textfield = document.createElement("input");


  1. Then assign necessary attributes to the variable.

    textfield.type = "text";

    textfield.value = "";


  1. At last append variable to the form element using appendChild method. so that the input element will be created in the form element itself.

    document.getElementById('form').appendChild(textfield);


  1. Loop the 2,3 and 4 step to create desired number of input elements given by the user inside the form element.

    for(var i=0;i<no;i++) {           
        var textfield = document.createElement("input");
        textfield.type = "text"; textfield.value = "";
        document.getElementById('form').appendChild(textfield);
    }
    

Here's the complete code

_x000D_
_x000D_
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);
    }
}
_x000D_
<form id="form">
    <input type="type" id="idname" oninput="fun()" value="">
</form>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to forms

How do I hide the PHP explode delimiter from submitted form results? React - clearing an input value after form submit How to prevent page from reloading after form submit - JQuery Input type number "only numeric value" validation Redirecting to a page after submitting form in HTML Clearing input in vuejs form Cleanest way to reset forms Reactjs - Form input validation No value accessor for form control TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"

Examples related to webforms

ASP.NET Button to redirect to another page How to use the DropDownList's SelectedIndexChanged event "Specified argument was out of the range of valid values" The ScriptManager must appear before any controls that need it Check if Cookie Exists How to do a Jquery Callback after form submit? "The given path's format is not supported." A potentially dangerous Request.Path value was detected from the client (*) How to create <input type=“text”/> dynamically Create a HTML table where each TR is a FORM