This is the HTML input:
<button id="addMore" class="button">+</button>
<div id="fieldList">
<input type="text" name="gsm[]" placeholder="GSM" required>
</div>
JavaScript:
$(function() {
$("#addMore").click(function(e) {
e.preventDefault();
$("#fieldList").append("<input type='text' name='gsm[]' placeholder='GSM' required>");
});
});
So when I press the + button, the new field will be added below the first one, and the values should be a array.
I have another button that runs this script:
$(function() {
$("#sendSamtykke").click(function(e) {
e.preventDefault();
var gsm = document.getElementById("gsm");
var gsm2 = "";
for (i = 0; i < gsm.length; i++) {
gsm2 += gsm[i] + "<br>";
}
alert(gsm2);
});
});
The alert should print the first input value, and a second line with the second input value since there are two boxes added, but no alert is showing up. What am I doing wrong? I want the text in both if the input boxes to show up, how can I do this correctly?
getElementById. In any case, as the name suggests, that returns a single element, not multiple elements. Instead usegetElementsByNameorquerySelectorAllthat return multiple elements.