<select name="pList" id="pList" style="display: none">
<option value="">--ProductA--</option>
<option value="productA">ProdA</option>
<option value="productA">ProdA</option>
<option value="productA">ProdA</option>
<option value="">--ProductB--</option>
<option value="productB">ProdB</option>
<option value="productB">ProdB</option>
<option value="">--ProducstC--</option>
<option value="productC">ProdC</option>
<option value="productC">ProdC</option>
<option value="productC">ProdC</option>
</select>
上記の選択値をアコーディオンに変換しようとしていますが、見出しの正しい値で新しいボタン要素を正常に作成できましたが、子として選択した値は、ボタン(アコーディオンのように動作するはずです)の内部で、そのちょうど見出しに値を追加し、正常に動作していない、参照してください。 https://jsfiddle.net/bernlt/txgnk89w/5/ボタンを子としてDivを追加します。
var element = document.getElementById('pList');
var children = element.children;
var filtered = [];
var filterItens = [];
// Create Headings for the Button e.g: --ProductA--/--ProductB--/--ProductC--
for (var i = 0; i < children.length; i++) {
if (children[i].textContent.startsWith('--')) {
filtered.push(children[i].textContent); //Heading labels
}else{
// separate array listing all products
filterItens.push(children[i].textContent);
}
}
var prods= filtered.toString().split(',');
var itens = filterItens.toString().split(',');
for (var i in prods) {
var newElement = document.createElement('button');
newElement.className = "accordion";
newElement.innerHTML = prods[i];
document.body.appendChild(newElement);
}
for (var i = 0; i < children.length; i++) { // get arraylist values
if (children[i].value=="productA"){
var foo = document.getElementById("--ProductA--");
var newElement = document.createElement('div');
newElement.id = children[i].value;
newElement.className = "productA";
newElement.innerHTML = children[i].text;
foo.appendChild(newElement);
}
if (children[i].value=="productB"){
var foo = document.getElementById("--ProductB--");
var newElement = document.createElement('div');
newElement.id = children[i].value;
newElement.className = "productB";
newElement.innerHTML = children[i].text;
foo.appendChild(newElement);
}
if (children[i].value=="productC"){
var foo = document.getElementById("--ProducstC--");
var newElement = document.createElement('div');
newElement.id = children[i].value;
newElement.className = "productC";
newElement.innerHTML = children[i].text;
foo.appendChild(newElement);
}
}
を私のように、他の提案に開いていますこれは私の学習の道であり、基本的にselectを使ってアコーディオンを作成しようとしているので、どんな助けもいいと思う。