using the DOM to change the CLASS attribute

S

silly putty

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>
 
N

nic.j.roche

document.getElementById('YO').getElementsByTagName("span")[0].className
= "hasEvents";
 
S

silly putty

didn't work

document.getElementById('YO').getElementsByTagName("span")[0].className
= "hasEvents";


silly said:
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>
 
M

Matt Kruse

silly said:
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]);
 
N

nic.j.roche

did you change the id of the TD to "YO"

silly said:
didn't work

document.getElementById('YO').getElementsByTagName("span")[0].className
= "hasEvents";


silly said:
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>
 
S

silly putty

Matt was right in that the ID can't begin with a digit. Thanks Matt
for figuring out the problem.

Just curious, (e-mail address removed) suggested that I change the ID to
Y0. what does Y0 mean? Thanks


Matt said:
silly said:
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]);
 
R

RobG

silly said:
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, (e-mail address removed) 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;
if ('hasEvents' == aSpan.className){
/* do stuff */
}
}
}

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

</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top