I have a number input field in which the user can enter a number. Depending on this number, several new input fields are generated.
Currently, I have no problem adding fields if the new number entered by the user is higher than the previous one. I cannot remove the generated fields if the user puts a smaller number though.
Thus, the user enters in '5', which generates 5 new fields. If the user adjusts this number to "3", there are still 5 generated fields.
HTML input
<input type="number" class="form-control" name="amount_of_vote_groups" id="amount_of_vote_groups">
Javascript (jQuery)
var i = 1;
$('#amount_of_vote_groups').change(function () {
while (i <= this.value) {
$('#right-column').prepend(
'<div class="form-group" id="generated-field-'+i+'">' +
'</div>'
);
i++;
}
if (this.value > i) {
$('#right-column').remove('#generated-field-' + i);
}
});
I know that I am doing something stupid with the operator remove, but I just can not understand. I am a real sucker in javascript;)