/    Sign up×
Community /Pin to ProfileBookmark

Navigating in the DOM problems…

I have a table structure, like this:

[code]
<table>
<tr><td><div>blah blah</div>blah blah</td></tr>
<tr><td><div>blah blah blah</div></td></tr>
<tr><td>blah</td></tr>
</table>
[/code]

What I want to do is a function that takes as an argument any element in this table and creates a class that stores the table as “table”, the first <tr> as “tBar”, the div in the second <tr> as “content” and the last <tr> as “sBar”. All works fine in Firefox but unfortunately, IE isn’t able to find the div from the second <tr>. Here’s what I have:

[code]
var activeObj = {
table: “”,
tBar: “”,
content: “”,
sBar: “”
}
function getTable(obj) {
if (obj.tagName.toLowerCase()==”table”) return obj;
else return getTable(obj.parentNode);
}
function fillObjClass(obj) {
activeObj.table = getTable(obj);
activeObj.tBar = activeObj.table.firstChild.nextSibling.firstChild;
activeObj.content = activeObj.table.getElementsByTagName(“DIV”)[1]; // IE says “Object required”
activeObj.sBar = activeObj.table.firstChild.nextSibling.lastChild.previousSibling;
}
[/code]

Please help… I’m running out of ideas…

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ZnupiauthorMar 06.2007 — Oh sorry, I removed all white space between tags (wrote the whole thing in one line) and now this works (and it's a lot more logical):
<i>
</i>function fillObjClass(obj) {
activeObj.table = getTable(obj);
activeObj.tBar = activeObj.table.firstChild.firstChild;
activeObj.content = activeObj.tBar.nextSibling.firstChild.firstChild;
activeObj.sBar = activeObj.table.firstChild.lastChild;
}
Copy linkTweet thisAlerts:
@JulesHMar 06.2007 — A more reliable way to do this (I've come across browsers inserting nodes that don't exist in the tree before now) would be to tag the divs with an 'id' attribute, and then use document.getElementById() to retrieve them, i.e.

<i>
</i>
&lt;table id='someTable'&gt;
&lt;tr&gt;&lt;td&gt;&lt;div id='someTableRow1'&gt;...&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;div id='someTableRow2'&gt;...&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
...
&lt;/table&gt;

...

function fillObjClass(obj)
{
activeObj.table = getTable(obj);
activeObj.tBar = document.getElementById(activeObj.table.id + 'Row1');
activeObj.content = document.getElementById(activeObj.table.id + 'Row2');
...
}


This would be more robust in case of changes or browser weirdness, and just requires you to generate a unique ID for each table in your document.
×

Success!

Help @Znupi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...