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>
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
Just curious, nic.j...@gmail.com suggested that I change the ID to
Y0. what does Y0 mean? Thanks
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