Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
using the DOM to change the CLASS attribute
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
silly putty  
View profile  
 More options Sep 21 2006, 1:59 pm
Newsgroups: comp.lang.javascript
From: "silly putty" <brendan.w...@gmail.com>
Date: 21 Sep 2006 10:59:59 -0700
Local: Thurs, Sep 21 2006 1:59 pm
Subject: using the DOM to change the CLASS attribute
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>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nic.j.roche@gmail.com  
View profile  
 More options Sep 21 2006, 3:06 pm
Newsgroups: comp.lang.javascript
From: "nic.j.ro...@gmail.com" <nic.j.ro...@gmail.com>
Date: 21 Sep 2006 12:06:47 -0700
Local: Thurs, Sep 21 2006 3:06 pm
Subject: Re: using the DOM to change the CLASS attribute
document.getElementById('YO').getElementsByTagName("span")[0].className
= "hasEvents";


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
silly putty  
View profile  
 More options Sep 21 2006, 5:05 pm
Newsgroups: comp.lang.javascript
From: "silly putty" <brendan.w...@gmail.com>
Date: 21 Sep 2006 14:05:37 -0700
Local: Thurs, Sep 21 2006 5:05 pm
Subject: Re: using the DOM to change the CLASS attribute
didn't work


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nic.j.roche@gmail.com  
View profile  
 More options Sep 21 2006, 6:01 pm
Newsgroups: comp.lang.javascript
From: "nic.j.ro...@gmail.com" <nic.j.ro...@gmail.com>
Date: 21 Sep 2006 15:01:49 -0700
Local: Thurs, Sep 21 2006 6:01 pm
Subject: Re: using the DOM to change the CLASS attribute
did you change the id of the TD to "YO"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Kruse  
View profile  
 More options Sep 21 2006, 5:52 pm
Newsgroups: comp.lang.javascript
From: "Matt Kruse" <newsgro...@mattkruse.com>
Date: Thu, 21 Sep 2006 16:52:51 -0500
Local: Thurs, Sep 21 2006 5:52 pm
Subject: Re: using the DOM to change the CLASS attribute

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
silly putty  
View profile  
 More options Sep 21 2006, 7:41 pm
Newsgroups: comp.lang.javascript
From: "silly putty" <brendan.w...@gmail.com>
Date: 21 Sep 2006 16:41:41 -0700
Local: Thurs, Sep 21 2006 7:41 pm
Subject: Re: using the DOM to change the CLASS attribute
Matt was right in that the ID can't begin with a digit.  Thanks Matt
for figuring out the problem.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RobG  
View profile  
 More options Sep 21 2006, 8:54 pm
Newsgroups: comp.lang.javascript
From: "RobG" <rg...@iinet.net.au>
Date: 21 Sep 2006 17:54:42 -0700
Local: Thurs, Sep 21 2006 8:54 pm
Subject: Re: using the DOM to change the CLASS attribute

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.ro...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »