Main Page

getAttribute

if (vValue1 < vValue2) {
return –1;
} else if (vValue1 > vValue2) {
return 1;
} else {
return 0;
}
};
}
Basically,
vValue1
and
vValue2
are defined to have no initial value. Then, you check to see if the cell in
the first row has a
value
attribute defined by using
getAttribute()
, which will return
null
when the
attribute doesn’t exist. When placed in the
if
statement,
null
is evaluated as
false
and a non-
null
value is evaluated as
true
. Therefore, if the
value
attribute exists, both
vValue1
and
vValue2
are
assigned the value of the attribute for
oTR1
and
oTR2
, respectively. If the
value
attribute doesn’t exist,
then
vValue1
and
vValue2
are assigned the value contained inside the table cell.
The only thing left to do is to add the sorting calls to the HTML code:
<table border=”1” id=”tblSort”>
<thead>
<tr>
<th onclick=”sortTable(‘tblSort’, 0)”
style=”cursor:pointer”>Type</th>
<th onclick=”sortTable(‘tblSort’, 1)”
style=”cursor:pointer”>Filename</th>
</tr>
</thead>
<tbody>
<tr>
<td value=”doc”><img src=”images/wordicon.gif”/></td>
<td>My Resume.doc</td>
</tr>
<tr>
<td value=”xls”><img src=”images/excelicon.gif”/></td>
<td>Fall Budget.xls</td>
</tr>
<tr>
<td value=”pdf”><img src=”images/acrobaticon.gif”/></td>
<td>How to be a better programmer.pdf</td>
</tr>
<tr>
<td value=”doc”><img src=”images/wordicon.gif”/></td>
<td>My Old Resume.doc</td>
</tr>
<tr>
<td value=”txt”><img src=”images/notepadicon.gif”/></td>
<td>Notes from Meeting.txt</td>
</tr>
<tr>
<td value=”zip”><img src=”images/zippedfoldericon.gif”/></td>
<td>Backups.zip</td>
</tr>
384
Chapter 12
15_579088 ch12.qxd 3/28/05 11:40 AM Page 384


JavaScript EditorFree JavaScript Editor     Ajax Editor


©