Reputation: 223
Simple question: I want to loop the array values in two dropdown lists in HTML using JavaScript. But only one dropdown is working. If I comment the one, the another one works. I want to fill the values in both the dropdown lists. What's wrong in this simple code?
var select = document.getElementById("selectNumber");
var select2 = document.getElementById("selectNumber2");
var options = ["1", "2", "3", "4", "5"];
for (var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
select2.appendChild(el);
}
<select id="selectNumber">
<option>Choose a number</option>
</select>
<select id="selectNumber2">
<option>Choose a number</option>
</select>
Upvotes: 18
Views: 2202
Reputation: 5690
You can use both JavaScript and jQuery:
var options = ["1", "2", "3", "4", "5"];
$.each(options, function(key, value) {
$('#selectNumber,#selectNumber2').append($("<option/>", {
value: key,
text: value
}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selectNumber">
<option>Choose a number1</option>
</select>
<select id="selectNumber2">
<option>Choose a number2</option>
</select>
This is JavaScript code:
<script>
var select = document.getElementById("selectNumber");
var select2 = document.getElementById("selectNumber2");
var options = ["1", "2", "3", "4", "5"];
for (var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
select2.appendChild(el.cloneNode(true));
}
});
</script>
This is other option if you want separate the option code
<script>
var select = document.getElementById("selectNumber");
var select2 = document.getElementById("selectNumber2");
var options = ["1", "2", "3", "4", "5"];
for (var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
var e2 = document.createElement("option");
e2.textContent = opt;
e2.value = opt;
select2.appendChild(e2);
}
</script>
Upvotes: 4
Reputation: 32354
Create a option element for each select,appendChild doesn't clone the element so the second append takes the element from the first
var select = document.getElementById("selectNumber");
var select2 = document.getElementById("selectNumber2");
var options = ["1", "2", "3", "4", "5"];
for (var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
var el1 = document.createElement("option");
el1.textContent = opt;
el1.value = opt;
select.appendChild(el1);
select2.appendChild(el);
}
<select id="selectNumber">
<option>Choose a number</option>
</select>
<select id="selectNumber2">
<option>Choose a number</option>
</select>
Upvotes: 9
Reputation: 5711
Do not use the same element for both controls. Clone it first
var select = document.getElementById("selectNumber");
var select2 = document.getElementById("selectNumber2");
var options = ["1", "2", "3", "4", "5"];
for (var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
select2.appendChild(el.cloneNode(true));
}
<select id="selectNumber">
<option>Choose a number</option>
</select>
<select id="selectNumber2">
<option>Choose a number</option>
</select>
Upvotes: 20
Reputation: 48427
Here is the method without clone (jsFiddle):
var select = document.getElementById("selectNumber");
var select2 = document.getElementById("selectNumber2");
var options = ["1", "2", "3", "4", "5"];
for (var i = 0; i < options.length; i++) {
var opt = options[i];
select.add(new Option(opt,opt));
select2.add(new Option(opt,opt));
}
Read more about the add
function for JavaScript select here.
Upvotes: 3
Reputation: 2713
var select = document.getElementById("selectNumber");
var select2 = document.getElementById("selectNumber2");
var options = ["1", "2", "3", "4", "5"];
for(var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
var el2 = document.createElement("option");
el2.textContent = opt;
el2.value = opt;
select2.appendChild(el2);
}
<select id="selectNumber">
<option>Choose a number</option>
</select>
<select id="selectNumber2">
<option>Choose a number</option>
</select>
Create two different nodes and add them.
Reason to why your code didn't work is here
Hope this helps :)
Upvotes: 7
Reputation: 467
The issue is the element you are creating can only be append in one parent. Not both. Bellow the code snippet that works
var select2 = document.getElementById("selectNumber2");
var options = ["1", "2", "3", "4", "5"];
for (var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
var copy = el.cloneNode(true);
select2.appendChild(copy);
}
Just notice the line var copy = el.cloneNode(true);
this create a new clone of the element, and then append it to another select
.
Upvotes: 2