I have two group of datalists. To iterate each of them this works:
“`
function validate() {
var inputs = document.querySelectorAll(“input[list]”);
inputs.forEach(element => {
alert(element.name)
});
}
But when trying to use the “variable” element.name to fetch the option elements of each group, it does not work.
“`
function validate() {
var inputs = document.querySelectorAll(“input[list]”);
inputs.forEach(element => {
alert(element.name)
var options = element.name.querySelectorAll(“option”);
options.forEach(option => {
if (option.value == element.value) {
alert(option.value + ” ” + option.dataset.id + ” data”)
}
});
});
}
Changing from
`var options = element.name.querySelectorAll(“option”);
to
`var options = browsers.querySelectorAll(“option”);
gets the options for ONE group.
The goal if this is to understand the forEach syntax. I know how to fix this with a standard for loop.
So how do I get this to work using _forEach