Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74020

How can I make a Dynamic Form Based on Names and Length of Array?

$
0
0

I have the following script taken from stackoverflow. It generates a number of input fields based on a number entered by the user. The problem is I need it to generate a number of input fields based the length of an array [loren, ipsum, plumpus, dumbas], and assign names to the fields that correspond to the strings in the array.

<html><head><script type='text/javascript'>
        function addFields(){
            // Number of inputs to create
            var number = document.getElementById("member").value;
            // Container <div> where dynamic content will be placed
            var container = document.getElementById("container");
            // Clear previous contents of the container
            while (container.hasChildNodes()) {
                container.removeChild(container.lastChild);
            }
            for (i=0;i<number;i++){
                // Append a node with a random text
                container.appendChild(document.createTextNode("Member " + (i+1)));
                // Create an <input> element, set its type and name attributes
                var input = document.createElement("input");
                input.type = "text";
                input.name = "member" + i;
                container.appendChild(input);
                // Append a line break 
                container.appendChild(document.createElement("br"));
            }
        }</script></head><body><input type="text" id="member" name="member" value="">Number of members: (max. 10)<br /><a href="#" id="filldetails" onclick="addFields()">Fill Details</a><div id="container"/></body></html>

Viewing all articles
Browse latest Browse all 74020

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>