I have two empty select fields, I want to iterate once over some array, create an option element in each iteration, and append it to both select fields. The problem is that only the last element gets the options, the first one remains empty:
HTML
<select class="form-control" id="typeCol">
</select>
<br>
<select class="form-control" id="diffCol">
</select>
JavaScript
let typeCol = document.getElementById('typeCol');
let diffCol = document.getElementById('diffCol');
for (let i in cols) {
let opt = document.createElement('option');
opt.value = i;
opt.innerHTML = cols[i];
typeCol.appendChild(opt);
diffCol.appendChild(opt);
}
Adding another for
loop and appending to the second select
from there seems to work, but still - what's going on?
I have two empty select fields, I want to iterate once over some array, create an option element in each iteration, and append it to both select fields. The problem is that only the last element gets the options, the first one remains empty:
HTML
<select class="form-control" id="typeCol">
</select>
<br>
<select class="form-control" id="diffCol">
</select>
JavaScript
let typeCol = document.getElementById('typeCol');
let diffCol = document.getElementById('diffCol');
for (let i in cols) {
let opt = document.createElement('option');
opt.value = i;
opt.innerHTML = cols[i];
typeCol.appendChild(opt);
diffCol.appendChild(opt);
}
Adding another for
loop and appending to the second select
from there seems to work, but still - what's going on?
3 Answers
Reset to default 9An element can only be inside one parent. If you use appendChild
on an element that already has a parent, it's moved from the old parent to the new one.
You can use cloneNode
to create a clone of the element instead:
diffCol.appendChild(opt.cloneNode(true));
Example:
let typeCol = document.getElementById('typeCol');
let diffCol = document.getElementById('diffCol');
let cols = ["one", "two", "three"];
for (let i in cols) {
let opt = document.createElement('option');
opt.value = i;
opt.innerHTML = cols[i];
typeCol.appendChild(opt);
diffCol.appendChild(opt.cloneNode(true));
}
<select class="form-control" id="typeCol">
</select>
<br>
<select class="form-control" id="diffCol">
</select>
You can't append same element, However you can use cloneNode() method to create a clone then append it.
typeCol.appendChild(opt);
diffCol.appendChild(opt.cloneNode(true));
//For this you can use template tag of HTML 5 :-
<select class="form-control" id="typeCol">
<template id="typeColTemplate">
<option id="" value=""></option>
</template>
</select>
<br>
<select class="form-control" id="diffCol">
<template id="diffColTemplate">
<option id="" value=""></option>
</template>
</select>
//In JS:-
for (let i in cols){
var count =1;
var content = document.querySelector("#typeColTemplate").content;
var content = document.querySelector("#typeDiffTemplate").content;
var option = content.querySelector("option[id]");
option.id="opID" + count;
option.value=i;
option.innerHTML=i;
document.querySelector('#typeCol').appendChild(document.importNode(content, true));
document.querySelector('#diffCol').appendChild(document.importNode(content, true));
count++;
}