473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

using the DOM to change the CLASS attribute

hello. i have a <tablewith 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').getElementsByTagNam e("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>

Sep 21 '06 #1
6 9267
document.getElementById('YO').getElementsByTagName ("span")[0].className
= "hasEvents";
silly putty wrote:
hello. i have a <tablewith 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').getElementsByTagNam e("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>
Sep 21 '06 #2
didn't work

ni*********@gmail.com wrote:
document.getElementById('YO').getElementsByTagName ("span")[0].className
= "hasEvents";
silly putty wrote:
hello. i have a <tablewith 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').getElementsByTagNam e("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>
Sep 21 '06 #3
did you change the id of the TD to "YO"

silly putty wrote:
didn't work

ni*********@gmail.com wrote:
document.getElementById('YO').getElementsByTagName ("span")[0].className
= "hasEvents";
silly putty wrote:
hello. i have a <tablewith 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').getElementsByTagNam e("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>
Sep 21 '06 #4
silly putty wrote:
didn't work
Could you be any more vague?
>>document.getElementById('0_1').getElementsByTagN ame("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').getElementsBy TagName("span"));
alert(document.getElementById('0_1').getElementsBy TagName("span")[0]);

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 21 '06 #5
Matt was right in that the ID can't begin with a digit. Thanks Matt
for figuring out the problem.

Just curious, ni*********@gmail.com suggested that I change the ID to
Y0. what does Y0 mean? Thanks
Matt Kruse wrote:
silly putty wrote:
didn't work

Could you be any more vague?
>document.getElementById('0_1').getElementsByTagNa me("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').getElementsBy TagName("span"));
alert(document.getElementById('0_1').getElementsBy TagName("span")[0]);

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 21 '06 #6
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, ni*********@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

Sep 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
2
by: Varun Singal | last post by:
Hi I am calling .NET components from my Classical ASPs. For this I strong named my assemblies, then registered them in the resistry ( using RegAsm), and put them in GAC (using GacUtil) ....
0
by: Jeff User | last post by:
Here is what I have and what I would like to do: I am building a web client application. I add a web reference to some web service server to my project so that now I can call the web service. A...
2
by: Zach Mortensen | last post by:
I can't seem to get dynamically-compiled JScript code to use C#-defined custom attributes. I have a simple attribute and a class defined in a C# assembly: namespace MyNamespace { public...
2
by: Jeff User | last post by:
Here is what I have and what I would like to do: I am building a web client application. I add a web reference to some web service server to my project so that now I can call the web service. A...
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
2
by: Nathan Sokalski | last post by:
I am attempting to create icons for controls I have created using VB.NET by using the System.Drawing.ToolboxBitmap attribute. I have managed to do this in C# by specifying the path to the *.ico...
5
by: MATTXtwo | last post by:
I have surf throught many site and found this... http://www.adobe.com/go/gn_home how to change an element attribute to other class attribute... I wonder......can we change class attribute to...
15
by: r0g | last post by:
Hi There, I know you can use eval to dynamically generate the name of a function you may want to call. Can it (or some equivalent method) also be used to do the same thing for the variables of a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.