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


Groups > comp.lang.javascript > #35471

Re: addEventListener question

Newsgroups comp.lang.javascript
Date 2018-08-12 11:12 -0700
References (4 earlier) <pkmroi$slk$1@solani.org> <2d0334bc-a32a-3be6-e0cd-fc55d7e7446c@PointedEars.de> <pkplrp$rrc$1@solani.org> <5205cdc9-fb48-884b-fe3a-88c6013824c4@PointedEars.de> <pkpra0$k4$1@solani.org>
Message-ID <11d0daaa-2ad3-4cf6-9564-9ab400cdca62@googlegroups.com> (permalink)
Subject Re: addEventListener question
From Julio Di Egidio <julio@diegidio.name>

Show all headers | View raw


On Sunday, 12 August 2018 19:38:14 UTC+2, Christoph M. Becker  wrote:

> My following simplified solution[1] is somewhat speculative,
> since it is based on some example HTML and an according script snippet,
> but anyway:
> 
>     document.getElementById("csschart")
>             .addEventListener("click", function (e) {
>         var elt = e.target;
>         while (elt && elt.tagName !== "DT") {
>                 while (elt.tagName !== "DD") {
>                     elt = elt.parentNode;
>             }
>             elt = elt.previousElementSibling;
>         }
>         if (elt) alert(elt.textContent - 1);
>     });

That is even more inefficient (and obscure) than the original code.

- To find the DTs, the easiest assuming there are no nested lists was to get
the root node by id, then use getElementsByTagName, otherwise set classes
and use getElementsByClassName... 

- The function to attach to the DT's click can be just one function and
written separately (adding better structure and clarity) as it does not need
any specific closure: a reference to the target element of the event (which
here is the DT, but the 'this' reference could be used) then to its properties
indeed is easy enough to get.

HTH,

Julio

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


Thread

addEventListener question Luuk <luuk@invalid.lan> - 2018-08-10 18:21 +0200
  Re: addEventListener question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-08-11 00:04 +0200
    Re: addEventListener question Luuk <luuk@invalid.lan> - 2018-08-11 14:28 +0200
      Re: addEventListener question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-08-11 14:53 +0100
        Re: addEventListener question "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-08-11 16:27 +0200
          Re: addEventListener question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-08-12 17:49 +0200
            Re: addEventListener question "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-08-12 18:05 +0200
              Re: addEventListener question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-08-12 18:42 +0200
                Re: addEventListener question "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-08-12 19:38 +0200
                Re: addEventListener question Julio Di Egidio <julio@diegidio.name> - 2018-08-12 11:12 -0700
                Re: addEventListener question James Kirk <noneya.invalid@gmail.com> - 2018-08-12 22:36 -0400
                Re: addEventListener question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-09-06 08:17 +0200
                Re: addEventListener question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-08-13 11:50 +0200
                Re: addEventListener question Luuk <luuk@invalid.lan> - 2018-08-18 16:50 +0200
        Re: addEventListener question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-08-11 16:58 +0200
          Re: addEventListener question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-08-12 02:40 +0100
            Re: addEventListener question "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-08-12 09:07 +0200
        Re: addEventListener question Luuk <luuk@invalid.lan> - 2018-08-11 19:17 +0200
  Re: addEventListener question "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-08-11 16:30 +0200
    Re: addEventListener question Luuk <luuk@invalid.lan> - 2018-08-11 19:12 +0200
    Re: addEventListener question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-08-12 17:46 +0200
      Re: addEventListener question "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-08-12 18:06 +0200
        Re: addEventListener question "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-08-12 18:12 +0200
          Re: addEventListener question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-08-12 18:33 +0200
  Re: addEventListener question Luuk <luuk@invalid.lan> - 2018-08-18 17:01 +0200

csiph-web