Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Retrieve a select from a table cell Date: Tue, 14 Jul 2015 22:02:35 +0200 Organization: PointedEars Software (PES) Lines: 69 Message-ID: <55A56ADB.60404@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Trace: solani.org 1436904342 15942 eJwNysEBwCAIA8CVUAnYcSCa/Uew9z6sGMH0QDgEWabCPnAK3H00hIJn19y4pltYOETz3875ACGxEZU= (14 Jul 2015 20:05:42 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 14 Jul 2015 20:05:42 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0 X-User-ID: eJwFwYEBACAEBMCVEp7GCd/+I3TnCkGHwWH+/JnUrp3QGKHZgnI0evLQh0uHeSWqV8tmmRyfrKsCXovn+EwwFYo= In-Reply-To: Cancel-Lock: sha1:TvK3CA2ptuyY2LnCqj8Gqh2+tFI= X-NNTP-Posting-Host: eJwFwQcBACAMAzBLO3QFObDjXwIJPDSSKxALg1HvLfaKxSsq5jfhcma/pnJIa1pVJjj7dH0TrBE4 Xref: csiph.com comp.lang.javascript:28420 Joao Rodrigues wrote: > DOMcreasel() may be simplified to: > > function DOMcreasel(tableID) { > var ns = document.createElement("SELECT"); > ns.options[ns.options.length] = new Option('Memo', 'M'); > […] > var lastrow = document.querySelector("#" + tableID > + ">tbody>tr:last-child"); > var i = lastrow.cells.length; > lastrow.insertCell(i).appendChild(ns); > } Yes, but then you would have to explain why you are combining the oldest proprietary with the newest standards-compliant features in an error-prone way. > You are adding onclick event handlers to every child of the TDs, and > fc.firstchild may be null depending on what you click (use Firefox / > Firebug to see it for yourself). Correct. But if that had been the reason here, the result would not have been an alert message, but a TypeError exception as “null as no properties”. > […] >> var fc=this.firstChild; >> if (fc) { >> alert(fc.type+" - "+fc.nodeName+" - "+fc.value+" - "+fc.nodeType) >> } >> […] >> >> When I click on the cell containing the first SELECT I have: >> undefined - #text - undefined - 3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> […] > > […] > var elems = tbl.querySelectorAll("input, select"); > for (var i = 0, len = elems.length; i < len; i++) { > elems[i].addEventListener('click', cellClick, false); > } The “click” event bubbles. Even though you are wasting much less memory than the OP, the loop and all those event listeners are unnecessary, especially if one assumes W3C DOM compatibility as you do. > } > > function cellClick(e) { > var fc = e.target; > window.alert(fc.type + " - " + fc.nodeName > + " - " + fc.value + " - " > + fc.nodeType); Insufficient, even by your own account: “select” elements have child elements. . . . . . . -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.