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


Groups > comp.lang.javascript > #13900

Re: Modify Element Tag Question

Message-ID <1402505.AFRqVoOGUt@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-05-13 15:50 +0200
Subject Re: Modify Element Tag Question
Newsgroups comp.lang.javascript
References <a158cjFt46U1@mid.individual.net> <4faf23ae$0$2892$a8266bb1@newsreader.readnews.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


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.

<http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>

>     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).

<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-58190037>

>     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.]

<http://www.w3.org/TR/1999/REC-html401-19991224/struct/
 global.html#adef-class>

>       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, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

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


Thread

Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-11 13:42 -0600
  Re: Modify Element Tag Question Martin Honnen <mahotrash@yahoo.de> - 2012-05-12 12:00 +0200
    Re: Modify Element Tag Question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-12 15:02 +0200
      Re: Modify Element Tag Question John G Harris <john@nospam.demon.co.uk> - 2012-05-12 16:15 +0100
        Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-12 14:32 -0600
  Re: Modify Element Tag Question Dr J R Stockton <reply1219@merlyn.demon.co.uk.not.invalid> - 2012-05-12 19:29 +0100
    Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-12 18:08 -0600
      Re: Modify Element Tag Question "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-05-13 08:11 +0000
    Re: Modify Element Tag Question Martin Honnen <mahotrash@yahoo.de> - 2012-05-13 11:25 +0200
      Re: Modify Element Tag Question Dr J R Stockton <reply1220@merlyn.demon.co.uk.not.invalid> - 2012-05-14 20:19 +0100
  Re: Modify Element Tag Question Denis McMahon <denismfmcmahon@gmail.com> - 2012-05-13 02:59 +0000
    Re: Modify Element Tag Question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-13 15:50 +0200
      Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-13 08:56 -0600
      Re: Modify Element Tag Question Dr J R Stockton <reply1219@merlyn.demon.co.uk.not.invalid> - 2012-05-14 20:01 +0100
    Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-13 09:00 -0600
      Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-14 08:45 -0600
        Re: Modify Element Tag Question Scott Sauyet <scott.sauyet@gmail.com> - 2012-05-14 16:31 -0700
          Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-15 09:03 -0600
        Re: Modify Element Tag Question Dr J R Stockton <reply1220@merlyn.demon.co.uk.not.invalid> - 2012-05-15 20:04 +0100
          Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-15 22:19 -0600
            Re: Modify Element Tag Question Dr J R Stockton <reply1220@merlyn.demon.co.uk.not.invalid> - 2012-05-17 19:13 +0100
              Re: Modify Element Tag Question Scott Sauyet <scott.sauyet@gmail.com> - 2012-05-17 18:48 -0700
                Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-18 13:14 -0600
                Re: Modify Element Tag Question Dr J R Stockton <reply1220@merlyn.demon.co.uk.not.invalid> - 2012-05-19 20:47 +0100
                Re: Modify Element Tag Question "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-05-20 08:31 +0000
                Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-20 18:25 -0600
                Re: Modify Element Tag Question "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-05-21 16:06 +0000
                Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-21 13:24 -0600
                Re: Modify Element Tag Question "Mel Smith" <med_cutout_syntel@aol.com> - 2012-05-21 13:34 -0600
                Re: Modify Element Tag Question Scott Sauyet <scott.sauyet@gmail.com> - 2012-05-21 13:09 -0700
                Re: Modify Element Tag Question Dr J R Stockton <reply1221@merlyn.demon.co.uk.not.invalid> - 2012-05-21 20:33 +0100

csiph-web