I'm trying to generate multiple <input type=text>
based on user's request. In another word, the user enters a number, like n
and therefore <input type=text>
going to appear on the screen for n
times.
Here is my code, which somehow is based on this answer, but doesn't work properly for me.
function generateInputs() {
var count = document.getElementById("test");
for (i = 0; i < Number(count); i++) {
var input = document.createElement("input");
input.setAttribute('type', 'text');
var x = document.getElementsByClassName("testClass");
x[0].appendChild(input)
}
}
<body>
<input type="number" id="test" min="2" max="20" required>
<button onclick="generateInputs()">Test</button>
<div class="testClass">
</div>
</body>
I expect to see for example 2 <input ...>
, if the user enters 2 and clicks the button, but nothing is shown.
Would you mind let me know about my mistake?