/    Sign up×
Community /Pin to ProfileBookmark

Nested forEach problem

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)
});
}
“`

https://jsfiddle.net/uk0m3och/

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”)
}
});
});
}
“`

https://jsfiddle.net/z6ftjg3k/2/

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_ loop?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumAug 02.2020 — `element.list` gets the corresponding datalist of an input. This works fine for me:

https://jsfiddle.net/Sempervivum/p3ctd0jb/1/
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...