Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using the DOM to change the CLASS attribute

24 views
Skip to first unread message

silly putty

unread,
Sep 21, 2006, 1:59:59 PM9/21/06
to
hello. i have a <table> with each cell having its own unique ID (see
below). What i want to do is change the CLASS attribute for the SPAN
in one of the cells. i'm currently testing this in IE 6. In my
javascript, i wrote

document.getElementById('0_1').getElementsByTagName("span")[0].setAttribute("className",
"hasEvents");

However, i get the following error message:

'document.getElementById(...).getElementsByTagName(...).0' is null or
not an object.

Can someone please help? thanks

HTML code:

<table>
<tr>
<td id="0_0"><span class="noEvents">1</span></td>
<td id="0_1"><span class="noEvents">2</span></td>
<td id="0_2"><span class="noEvents">3</span></td>
</tr>
</table>

nic.j...@gmail.com

unread,
Sep 21, 2006, 3:06:47 PM9/21/06
to
document.getElementById('YO').getElementsByTagName("span")[0].className
= "hasEvents";

silly putty

unread,
Sep 21, 2006, 5:05:37 PM9/21/06
to
didn't work

nic.j...@gmail.com

unread,
Sep 21, 2006, 6:01:49 PM9/21/06
to
did you change the id of the TD to "YO"

Matt Kruse

unread,
Sep 21, 2006, 5:52:51 PM9/21/06
to
silly putty wrote:
> didn't work

Could you be any more vague?

>>> document.getElementById('0_1').getElementsByTagName("span")[0].setAttribute("className",
>>> "hasEvents");

An ID attribute value cannot start with a digit. Your ID is invalid, so
results of the getElementById() call are unpredictable.

One way of debugging your own problem is to do an alert at each stage to see
where your problem is happening:

alert(document.getElementById('0_1'));
alert(document.getElementById('0_1').getElementsByTagName("span"));
alert(document.getElementById('0_1').getElementsByTagName("span")[0]);

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


silly putty

unread,
Sep 21, 2006, 7:41:41 PM9/21/06
to
Matt was right in that the ID can't begin with a digit. Thanks Matt
for figuring out the problem.

Just curious, nic.j...@gmail.com suggested that I change the ID to
Y0. what does Y0 mean? Thanks

RobG

unread,
Sep 21, 2006, 8:54:42 PM9/21/06
to
silly putty wrote:
> Matt was right in that the ID can't begin with a digit. Thanks Matt
> for figuring out the problem.

Please don't top-post here, reply below a trimmed quote of whatever you
are replying to.

>
> Just curious, nic.j...@gmail.com suggested that I change the ID to
> Y0. what does Y0 mean?

Nothing, it just means your ID doesn't start with a digit.

Incidentally, since you are looking for spans within the table, have
you consdered using an ID on the table and just getting all the spans
once? If you base the function on just getting an element reference,
you can re-use the code for any kind of element.

e.g.

<table id="theTable">
<tr>
<td><span class="noEvents">...</span>.
<td><span class="hasEvents">...</span>
<td><span class="noEvents">...</span>
</tr>
</table>

<script type="text/javascript">
function doSpanStuff(el){
var allSpans = el.getElementsByTagName('span');
var i = allSpans.length;
var aSpan;
while (i--){
aSpan = allSpans[i];
if ('hasEvents' == aSpan.className){
/* do stuff */
}
}
}

doSpanStuff(document.getElementById('theTable'));

</script>


--
Rob

0 new messages