i'm working on a form and having some problems.
I have a input field that when changed to a higher or lower number it adds/removes extra fields but if type something into one of the extra fields and add an additional one it removes all values where it should be saving the values
js:
var eh = document.getElementById('ulId');//list to add list items to
var ac = document.getElementById('inputId');
var defaultId = "xin";
//x is short extra
var anyXFields = false;
var XF = 0;//extra fields
var lastXField = 0;
var mostFields = 0;
var index = [];
function hideInputFields(n) {
if (lastXField < n) {
return;
}else if(lastXField > n) {
$("#xin" + lastXField).hide();
}
}
//on change function
function oc() {
n = ac.value;
n = parseInt(n);
lastXField = n - 1;
if (n > 0) {
if (anyXFields) {
hideInputFields(n - 2);
}
for (i = 0; i < (n - 1); i++) {
if (i < mostFields) {
$("#xin" + i).show();
} else if (!index[i]) {
eh.innerHTML += '<li id="'+(defaultId + i)+'"><label>Højde' + (i + 2) +' <label class="sub">Height' + (i + 2) +'</label></label><input name="'+(defaultId + i)+'" class="inputfield"></li>';
index[i] = 1;
}
}
if (mostFields < (n - 1)) {
mostFields = n - 1;
}
anyXFields = true;
}
}