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:Xns9FB6521286...@94.75.214.39>