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


Groups > comp.lang.javascript > #28425

Re: Retrieve a select from a table cell

Path csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it>
Newsgroups comp.lang.javascript
Subject Re: Retrieve a select from a table cell
Date Wed, 15 Jul 2015 09:10:06 +0200
Organization A noiseless patient Spider
Lines 86
Message-ID <mo50sg$2kh$1@dont-email.me> (permalink)
References <mo2utl$9s5$1@dont-email.me> <1620289.04d1OFRnt4@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
Injection-Date Wed, 15 Jul 2015 07:08:00 +0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="93dc01ef9af982d6d69cb0207cd22879"; logging-data="2705"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cC2L6D3wiaMGrbcDC2Laa"
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.0.1
In-Reply-To <1620289.04d1OFRnt4@PointedEars.de>
Cancel-Lock sha1:MgHk0k53asV/W4le0wvg1cLG3xE=
Xref csiph.com comp.lang.javascript:28425

Show key headers only | View raw


Il 07/14/2015 09:36 PM, Thomas 'PointedEars' Lahn ha scritto:

<...> (many corrections on details, [down to the point to argue about 
the name I assigned to a variable - come on! :-)]
and some interesting suggestions:
I was in a hurry, I wrote that Q&D piece of code just to post the 
message, hoping to find some answers when back again to my PC.

>
>>      table.rows[f].cells[g].onclick=function(){
>>       var fc=this.firstChild;
>>       if (fc) {
>>        alert(fc.type+" - "+fc.nodeName+" - "+fc.value+" - "+fc.nodeType)
>>       }
>>      }
>
> Note that creating functions in a loop is error-prone because a function
> creates a closure; JSHint can warn you about this.
>
> The loop and creating functions in a loop is even unnecessary here because
> the “click” event bubbles: you can listen to it at the row (“tr”), row group
> (“thead” or “tbody” element), or the table (“table” element) instead,
> requiring much less event listeners (down to only *one* at the table),
> therefore much less memory (each function definition creates an object).
> You are creating table.rows.length * table.rows[0].cells.length Function
> instances here, assuming that each row of the table has the same number of
> cells as the first one.
>
> The “target” or “srcElement” property of the event object would then yield
> the reference to the event target.  There is only one caveat: If the event
> target is a descendent of a cell, you need to walk up the DOM tree until you
> find the cell.  But because the “click” event bubbles, you need to do that
> in any case, and you neglected it here.
>
>    ….onclick = function (e) {
>      var target = e.target || window.event.srcElement;
>
>      if (!target) return true;
>
>      while (target && target.tagName && target.tagName.toLowerCase() != "td")
>      {
>        target = target.parentNode;
>      }
>
>      if (target && target.tagName)
>      {
>        /* work with target */
>      }
>    };
>
>>     }
>>    }
>> }

Interesting. Thanks, I did not even think about event.target or 
window.event.srcElement.

>
> Among other things, “document.forms["…"].elements["datatype"]”.
>
> The question is, why are you adding a “click” event listener on the cell in
> the first place?` And why do you think you need to refer to the form control
> in this complicated way?
>

You have been very precise and useful, but I must say that the fragments 
I posted are just a couple of tries I made to understand why I was not 
able to get the <select> values. To be honest, I started with an example 
I found on the net, in which new rows are created reading the HTML 
existing table, and just putting in the cells after insertCell() the 
innerHTML read from the existing cells. I assumed that createElement() 
and appendChild() is a more precise and compatible way to do it and 
discovered that this way I can access the <select> values. Thanks to 
Christoph Becker I realized that the formatting spaces are present in 
the DOM so I was not reading the <select> control at all.

The onclick() is only a quick way to display via alert() the fetched 
values, it is nothing more than a (for internal use only) test.

Now I am still trying to get your first answer, the one about properties 
via prototype chain and via object reference - a bit obscure to me. :-(

Dan
-- 
"Everybody should pay taxes with a smile"
I tried, but they want money.

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


Thread

Retrieve a select from a table cell "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-14 14:24 +0200
  Re: Retrieve a select from a table cell "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-07-14 14:45 +0200
    Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-14 16:54 +0200
      Re: Retrieve a select from a table cell "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-07-14 23:20 +0200
    Re: Retrieve a select from a table cell "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 09:11 +0200
  Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-14 21:36 +0200
    Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-14 21:41 +0200
    Re: Retrieve a select from a table cell "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 09:10 +0200
      Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 09:46 +0200
        Re: Retrieve a select from a table cell "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-07-15 10:03 +0200
          Re: Retrieve a select from a table cell "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-07-15 06:33 -0700
        Re: Retrieve a select from a table cell "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 10:17 +0200
          Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 11:47 +0200
            Re: Retrieve a select from a table cell "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 13:11 +0200
              Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-18 18:04 +0200
  Re: Retrieve a select from a table cell Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-07-14 16:44 -0300
    Re: Retrieve a select from a table cell Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-07-14 17:03 -0300
      Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-14 22:07 +0200
        Re: Retrieve a select from a table cell Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-07-14 17:28 -0300
          Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 00:23 +0200
    Re: Retrieve a select from a table cell Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-14 22:02 +0200

csiph-web