Path: csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.150.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Multiple inputs with same event Date: Tue, 09 Jun 2020 02:49:46 +0200 Organization: PointedEars Software (PES) Lines: 59 Message-ID: <1772720.tdWV9SEqCh@PointedEars.de> References: <9KednS9A0scWq0HDnZ2dnUU7-TnNnZ2d@westnet.com.au> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="150.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.236.150"; logging-data="30764"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.10 Cancel-Lock: sha1:T1lcEhb0a443GrWRhnYrdo70YRA= X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. On Saturday, June 6, 2020 at 7:36:36 PM UTC-5, Andrew Poulos wrote: >> Is there a way for multiple inputs to call the same function when the >> same event occurs to any of them without directly attaching the event to >> each input? Or should a just write a function that dynamically attaches >> the required events? > > If you wrap all of your elements in a
tag _“form” element_ Elements are NOT tags. > you can attach an > eventListener to that form and capture any "change" event: > > ```html > > > > > > ``` > > ```js > let myForm = document.getElementById('myForm') > > myForm.addEventListener('change', (e) => { > console.log(e) > }) > ``` This code should only be executed once it has been ascertained that the relevant parts of the document have been parsed to produce a document tree. It is actually not necessary (but recommended by some people) do this with assignment with scripting. However, a form element can have an “onchange” event-handler attribute that can execute code, like a function:
…
Then the form element does not need an “id” attribute, and the reference to object representing the current propagated event and to the object representing the form element, can be obtained by passing the “event” and “this” values respectively. Note that the “change” event might not be fired for every control. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.