Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38458

Re: Multiple inputs with same event

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Multiple inputs with same event
Date 2020-06-09 02:49 +0200
Organization PointedEars Software (PES)
Message-ID <1772720.tdWV9SEqCh@PointedEars.de> (permalink)
References <9KednS9A0scWq0HDnZ2dnUU7-TnNnZ2d@westnet.com.au> <ef363a56-281b-49ab-9e32-e244a27411d1o@googlegroups.com>

Show all headers | View raw


Michael Haufe (TNO) wrote:

> 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 <form> tag

_“form” element_

Elements are NOT tags.

<https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Getting_started>

<https://www.w3.org/TR/html52/introduction.html#a-quick-introduction-to-html>

> you can attach an
> eventListener to that form and capture any "change" event:
> 
> ```html
> <form id="myForm">
>   <input name="a">
>   <input name="b">
>   <input name="c">
> </form>
> ```
> 
> ```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:

  <form … onchange='my.change(this, event)'>
    …
  </form>

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: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Multiple inputs with same event Andrew Poulos <ap_prog@hotmail.com> - 2020-06-07 10:36 +1000
  Re: Multiple inputs with same event Arno Welzel <usenet@arnowelzel.de> - 2020-06-07 11:06 +0200
    Re: Multiple inputs with same event "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-06-07 13:09 +0200
      Re: Multiple inputs with same event Luuk <luuk@invalid.lan> - 2020-06-07 15:40 +0200
        Re: Multiple inputs with same event "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-06-08 00:37 +0200
          Re: Multiple inputs with same event Julio Di Egidio <julio@diegidio.name> - 2020-06-08 07:44 -0700
  Re: Multiple inputs with same event James Kirk <noneya.invalid@gmail.com> - 2020-06-07 10:30 -0400
  Re: Multiple inputs with same event JJ <jj4public@vfemail.net> - 2020-06-07 23:49 +0700
  Re: Multiple inputs with same event "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-06-07 12:56 -0700
    Re: Multiple inputs with same event Andrew Poulos <ap_prog@hotmail.com> - 2020-06-08 11:39 +1000
      Re: Multiple inputs with same event Gregor Kofler <usenet@gregorkofler.com> - 2020-06-08 11:33 +0200
    Re: Multiple inputs with same event Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-06-09 02:49 +0200
      Re: Multiple inputs with same event Andrew Poulos <ap_prog@hotmail.com> - 2020-06-10 17:19 +1000
        Re: Multiple inputs with same event Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-06-10 17:12 +0200
          Re: Multiple inputs with same event Andrew Poulos <ap_prog@hotmail.com> - 2020-06-11 15:43 +1000

csiph-web