/    Sign up×
Community /Pin to ProfileBookmark

dynamic select not having event listener added

Loading select elements based on a database query. Trying to add an onchange event so i can do a fetch query when a selection is made. When I console log my dropdowns i get them, but when i test an on change nothing.

Why?

“`
const dropdowns = document.querySelectorAll(‘select’);
for (i = 0; i < dropdowns.length; i++) {
//console.log(dropdowns[i]);
dropdowns[i].addEventListener(‘onchange’, function() {
console.log(“test”)
});
}
“`

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorJan 04.2023 — When I run the page and leave it, I get this after a while

> Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received
Copy linkTweet thisAlerts:
@cootheadJan 04.2023 — Hi there kiwis80,

I think that you will find that this...
<i>
</i>dropdowns[i].addEventListener('onchange', function() {

...shold actually be...
<i>
</i>dropdowns[i].addEventListener('change', function() {


[i]coothead[/i]
Copy linkTweet thisAlerts:
@kiwisauthorJan 05.2023 — Oh awesome,

How can I pass in the actual element so I can get the ID and selected value?
Copy linkTweet thisAlerts:
@SempervivumJan 05.2023 — @kiwis80#1649658 Javascript passes the event as a parameter automatically. In `event.target</C> the element that has triggered the event is available and you can read it's properties:
<CODE>
`<i>
</i> dropdowns[i].addEventListener('change', function(event) {
console.log(event.target.value);
console.log(event.target.id);
});<i>
</i>
``
×

Success!

Help @kiwis 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 4.25,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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