Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing table cell w/o an ID 1

Status
Not open for further replies.

greedyzebra

Technical User
Joined
Sep 5, 2001
Messages
140
Location
US
In the following code:

<html>
<head>
<style type=&quot;text/css&quot;>
.hideMe {display : none}
</style>
</head>
<body>
<table border=&quot;0&quot; width=&quot;60%&quot; id=&quot;theTable&quot;>
<tr>
<td width=&quot;50%&quot;><input type=&quot;button&quot; value=&quot;Click Here&quot; onclick=&quot;document.getElementById('tCell1').onclick()&quot;></td>
<td width=&quot;50%&quot; class=&quot;hideMe&quot; onclick=&quot;this.style.display='inline'&quot; id=&quot;tCell1&quot;>Data for cell 1</td>
</tr>
<tr>
<td width=&quot;50%&quot;><input type=&quot;button&quot; value=&quot;Click Here&quot; onclick=&quot;document.getElementById('tCell2').onclick()&quot;></td>
<td width=&quot;50%&quot; class=&quot;hideMe&quot; onclick=&quot;this.style.display='inline'&quot; id=&quot;tCell2&quot;>Data for cell 2</td>
</tr>
<tr>
<td width=&quot;50%&quot;><input type=&quot;button&quot; value=&quot;Click Here&quot; onclick=&quot;document.getElementById('tCell3').onclick()&quot;></td>
<td width=&quot;50%&quot; class=&quot;hideMe&quot; onclick=&quot;this.style.display='inline'&quot; id=&quot;tCell3&quot;>Data for cell 3</td>
</tr>
<tr>
<td width=&quot;50%&quot;><input type=&quot;button&quot; value=&quot;Click Here&quot; onclick=&quot;document.getElementById('tCell4').onclick()&quot;></td>
<td width=&quot;50%&quot; class=&quot;hideMe&quot; onclick=&quot;this.style.display='inline'&quot; id=&quot;tCell4&quot;>Data for cell 4</td>
</tr>
</table>
</body>
</html>

I would think that instead of having to create a separate id for each table cell in the &quot;hideMe&quot; class I could access what appears to be the next node in the document tree (the table cells are part of the same table row, right?).

If that doesn't make sense, I guess my question boils down to this, how can I affect the style attributes of cell 2 by clicking the button in cell 1 without having to have the unique id's for the cells not containing the button?

Thanks.
 
by what other criteria do you think you want to locate the target DOM element?

-pete
I just can't seem to get back my IntelliSense
 
I've tried to access child nodes, but that didn't work.

Don't know, really. I guess that's oen of the reasons for the post :)

Since, in my projects, I've used CGI scripts that produce tables, the burden of creating dozens (in some cases hundreds) of unique ids has not been a problem; the script does it. However, in a situation where I would have to &quot;hand code&quot; the ids, it would be a little bit of a pain.

Just thought there might be a better way...
 
All documentation I've ever read says you cannot rely on the order that items appear in collections in the DOM... for instance, items do not appear in any particular order in the form.elements[] collection, even though it would make sense that they would be in the same order as they were declared.

However, some places do adhere to the &quot;order-defined&quot; criteria, like with <div>'s, those do appear sequentially in the &quot;all&quot; collection of the document object (to the best of my knowledge).

As such, since its definitely not a declared and definite thing, you're going to have spotty browser-dependent behavior with an assumption like that. Best way to achieve this is to have a unique ID. Hope this helps clarify. :)
 
Thanks, shade. I played around with accessing parent and child nodes, but I think, as you explained, that the elements collection's not necessarily being in order prevented any success.

I thought of the idea because I thought not having to have unique ids would save some typing, but I think there would be more typing involved if I tried to access the elements without the ids!

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top