Why My Input's Value Get Cleared When I Append A Child?
I'm having trouble when I append an element inside a div and is that all the values from my inputs (including selected options) get cleared as is shown here: As you can see when I
Solution 1:
The problem is that you overwrite the HTML code
element.innerHTML += newHTML;
This gets rid of the state of the current elements, including event listeners, input values, checkedness, etc.
Instead, you should use appendChild
or insertAdjacentHTML
:
element.insertAdjacentHTML('beforeend', newHTML);
var controls = [{value: "flechas", text: "Flechas de movimiento"}, {value: "letras", text: "Letras (WASD)"}, {value: "enter", text: "Enter"}, {value: "control", text: "Ctrl"}, {value: "alt", text: "Alt"}, {value: "espacio", text: "Espacio"}, {value: "der", text: "Click derecho"}, {value: "izq", text: "Click izquierdo"}, {value: "mover", text: "Mover el ratón"}, {value: "shift", text: "Shift"}, {value: "customkey", text: "Especificar tecla"}],
skippedIndexes = [],
sep = "<div class='sep' id='firstsep' style='width: 200px;'></div>",
oldIndex = 0; //,//sInd = 0;functionaddControl(e) {
if (e.lastChild.className != "sep") e.innerHTML += sep;
e.appendChild(getControlSelect());
e.insertAdjacentHTML('beforeend', sep.replace(" id='firstsep'", ""));
}
functiononSelectChange(e) {}
functiondeleteFirstSep() {
if (!document.getElementById("firstsep").nextSibling) document.getElementById("firstsep").remove();
}
functiongetControlSelect() {
var select = document.createElement('select'),
option,
i = 0,
il = controls.length,
html = document.createElement('div'),
text = document.createElement('input'),
delbtn = document.createElement('input'),
ascinpt = document.createElement('input'),
html1 = document.createElement('div');
html1.style.display = "inline-block";
text.type = "text";
text.name = "accion[]";
text.style.width = "158px";
delbtn.type = "button";
delbtn.setAttribute('onclick', 'var e = this.parentNode;e.nextSibling.remove();e.remove();deleteFirstSep();');
delbtn.value = "-";
delbtn.style.padding = "0 5px";
delbtn.style.marginLeft = "5px";
delbtn.style.position = "relative";
delbtn.style.top = "-20px";
delbtn.style.left = "2px";
ascinpt.name = "tecla[]";
ascinpt.type = "hidden";
select.id = "htmlkey";
select.dataset.associatedInput = ascinpt;
//select.dataset.i = sInd;//sInd++;
select.setAttribute("onchange", "onSelectChange(this)");
for (; i < il; ++i)
if (skippedIndexes.indexOf(i) == -1) {
option = document.createElement('option');
option.setAttribute('value', controls[i].value);
option.appendChild(document.createTextNode(controls[i].text));
select.appendChild(option);
}
html1.insertAdjacentHTML('beforeend', "<b>Tecla</b><br>");
html1.appendChild(select);
html1.insertAdjacentHTML('beforeend', "<br><b>Acción</b><br>");
html1.appendChild(text);
html.appendChild(html1);
html.appendChild(delbtn);
return html;
}
.sep {
border-top: 4px dashed #A4A4A4;
margin: 15px015px0;
}
<div><inputtype="button"value="Añadir tecla"onclick="addControl(this.parentNode)" /></div>
Post a Comment for "Why My Input's Value Get Cleared When I Append A Child?"