Path: csiph.com!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1402505.AFRqVoOGUt@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 13 May 2012 15:50:16 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <4faf23ae$0$2892$a8266bb1@newsreader.readnews.com> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 134 NNTP-Posting-Date: 13 May 2012 15:50:18 CEST NNTP-Posting-Host: 919864d9.newsspool2.arcor-online.net X-Trace: DXC=11lfa7f3H2R\PS5Xo=M[RVA9EHlD;3YcR4Fo<]lROoRQ8kFKPfQ>EOE\[m`[Bl89g_ekZaU X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:13900 Denis McMahon wrote: > On Fri, 11 May 2012 13:42:09 -0600, Mel Smith wrote: >> In my phone book (of 613 multi-line entries in a table) >>, I am >> currently >> 'bolding' the names of the people, and leaving other fields 'unbolded'. >> >> Now, when a user decides to change the sorting order to street >> address, >> I wish to 'unbold' the names, and bold the street addresses. > > Hmm If only you would stop right there, posterity would be saved a lot of precious bandwidth that they are going to need desperately. > The way I think I would do it, assuming that each row has a series of td > elements .... > > To the td elements that contain names, I would add 'class="name"', and to > the td elements that hold addresses, I would add 'class="addr"' So much, so good. > Then, as an appendage to the sort routines, call something like boldCells > as shown below with either "name" or "addr" (it can be expanded to other > fields if needed): How do you manage coming up so easily with the worst "solutions" possible? Did V'Often Wrong'K teach you? > function boldCells( embolden ) { > var cells = document.getElementsByTagName( "td" ); That inefficient, error-prone method call is unnecessary. HTMLTableElement objects have a `rows' property, and HTMLTableRowElement objects have a `cells' property, that hold a reference to a NodeList of references to HTMLTableRowElement objects and HTMLTableCellElement objects, respectively. > var ix = cells.length; > var cell, class; `class' is a future reserved word; it cannot be the name of a variable as that must be an *identifier* (see the ECMAScript Language Specification, 5.1 Edition, section 7.5.3, but also about any JS tutorial out there, for any common meaning of "JS" being a programming language). As result, this code does not compile, and does not run. For example, in Chromium 18.x: | > var class = 42; | SyntaxError: Unexpected reserved word > while ( ix-- ) { > cell = cells.item( ix ); This rather expensive and potentially incompatible call of the `item' method of the `cells' NodeList object is not necessary, you can simply write cells[ix] instead. > class = cell.attributes.getNamedItem( "class" ); OMG. As regulars will know, an HTMLElement object has a `className' property. A simple cell.className does it all, and is a lot more compatible than your bloatcode (even the equally bloated and equally wrong cell.getAttribute("class") is more efficient than that). > if ( class.indexOf( "name" ) != -1 ) { This is insufficient to determine if the attribute value contains a certain class name, because it will result in false positives if a class name is, for example, "foonamebar". (Besides, the index of a substring can never be negative, so you should lose the loose comparison and use `> -1' instead.) We have already discussed here ad nauseam how to determine efficiently and reliably if a CSS class name is in a `class' attribute value. For those who have not been paying attention, or are new here, the consensus reached in those discussions was: if (/(^|\s)name(\s|$)/.test(cell.className)) { … } [`\b' as delimiter does not suffice, because (CSS) class names may contain non-ASCII characters, which are considered non-word characters. `\s' should suffice but does not match HTML's white-space definition exactly in all implementations; so a custom character class might be used instead.] > if ( embolden == "name" ) > cell.style.fontWeight = "bold"; > else > cell.style.fontWeight = "normal"; > } > else if ( class.indexOf( "addr" ) != -1 ) { > if ( embolden == "addr" ) > cell.style.fontWeight = "bold"; > else > cell.style.fontWeight = "normal"; > } Or you could simply switch the class name. foo.className = "bold"; > } > > Note - code is untested! … and written cluelessly, like everything else coming from you before. Not just in this newsgroup. PointedEars -- Sometimes, what you learn is wrong. If those wrong ideas are close to the root of the knowledge tree you build on a particular subject, pruning the bad branches can sometimes cause the whole tree to collapse. -- Mike Duffy in cljs,