最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Appending the same element to different elements - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Feb 17, 2017 at 13:17 Hywel Rees 9141 gold badge15 silver badges22 bronze badges asked Feb 17, 2017 at 12:27 user99999user99999 2,0245 gold badges27 silver badges51 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

An 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++;
}
发布评论

评论列表(0)

  1. 暂无评论