0

I have an object with information. I want to be able to create multiple of the same 2 input fields in html based on the object's number in a specific attribute.

So if the object contains the number 4, i want to write these 2 lines, 4 times.

<input type="text" placeholder="Enter First Name" />
<input type="text" placeholder="Enter Last Name" />

Is there any easy way to do that in javascript/jquery?

Rainoa
  • 491
  • 1
  • 4
  • 14

1 Answers1

0

You can try something like this.

        function addFields(){
            var number = document.getElementById("member").value;
            var container = document.getElementById("container");
            while (container.hasChildNodes()) {
                container.removeChild(container.lastChild);
            }
            for (i=0;i<number;i++){
                container.appendChild(document.createTextNode("Member " + (i+1)));
                var input = document.createElement("input");
                input.type = "text";
                container.appendChild(input);
                container.appendChild(document.createElement("br"));
            }
        }
<input type="text" id="member" name="member" value="">Number of times<br />
<a href="#" id="filldetails" onclick="addFields()">Fill Details</a>
    <div id="container"/>

You can see the answer here

Community
  • 1
  • 1
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140