When loading an HTML table, there is this select
input field, which should make available some options.
Right after the HTML
table row is built, I’m calling a function, which should populate this input field, getting it by its class
.
Here is the HTML
piece and the function:
JavaScript
x
20
20
1
function loadTelaOptions(telaOptions) {
2
telaOptions = ["09-Black", "11-LT Jaspe"]; //Here for tests
3
document.querySelector('tableBodySelect').value = '';
4
5
let selectList = document.querySelector('tableBodySelect');
6
let options = selectList.getElementsByTagName('option');
7
for (let i = options.length; i--;) {
8
selectList.removeChild(options[i]);
9
}
10
let option = document.createElement("option");
11
option.value = "";
12
option.text = "";
13
selectList.appendChild(option);
14
telaOptions.forEach(function(item, index) {
15
let option = document.createElement("option");
16
option.value = item.toLowerCase();
17
option.text = item;
18
selectList.appendChild(option)
19
});
20
}
JavaScript
1
4
1
<td>
2
<select name='tableselect' required='required' class='tableBodySelect' value='Trying'>
3
</select>
4
</td>
Appreciate any help!
Advertisement
Answer
Your selectors for .querySelector('tableBodySelect')
doesn’t match your desired elements. You try to match the classes, therefore you need to prepend a .
to your selector .querySelector('.tableBodySelect')
This does already resolve your issue.
JavaScript
1
21
21
1
function loadTelaOptions(telaOptions) {
2
telaOptions = ["09-Black", "11-LT Jaspe"]; //Here for tests
3
document.querySelector('.tableBodySelect').value = '';
4
5
let selectList = document.querySelector('.tableBodySelect');
6
let options = selectList.getElementsByTagName('option');
7
for (let i = options.length; i--;) {
8
selectList.removeChild(options[i]);
9
}
10
let option = document.createElement("option");
11
option.value = "";
12
option.text = "";
13
selectList.appendChild(option);
14
telaOptions.forEach(function(item, index) {
15
let option = document.createElement("option");
16
option.value = item.toLowerCase();
17
option.text = item;
18
selectList.appendChild(option)
19
});
20
}
21
loadTelaOptions();
JavaScript
1
4
1
<td>
2
<select name='tableselect' required='required' class='tableBodySelect' value='Trying'>
3
</select>
4
</td>