Forum Moderators: open

Message Too Old, No Replies

DOM (HTML) vs. DOM (CORE)

         

daosmith

4:08 am on Jan 22, 2007 (gmt 0)

10+ Year Member



I often find myself in situations where it is more convenient to use the old level 1 DOM HTML objects and methods to access some element in a HTML document rather than using the pure 'core' methods e.g.

someTable = document.getElementById("exampleTable");
rows = someTable.tBodies[0].rows

vs.

someTable = document.getElementById("exampleTable");
tBodies = someTable.getElementsByTagName("tbody");
rows = tBodies[0].getElementsByTagName("tr");

Given that the HTML DOM is not a feature of the level 2 and 3 recommendations I would guess that using the core methods, although longer, is the preferred (futureproof) route.

I am interested to see to what degree (if at all) other people agree, especially so given that there are some facilities in the HTML DOM (getting the indices of selected options in a select-list, for example) that are really quite difficult to replicate using the core methods only. So, which approach do you use, and why?

daveVk

5:23 am on Jan 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rows = someTable.childNodes[0].childNodes

Not as self documenting, but assume complies to later standards?

or should it be
rows = someTable.childNodes[ 1 ].childNodes

[edited by: daveVk at 5:25 am (utc) on Jan. 22, 2007]

daosmith

1:52 am on Jan 23, 2007 (gmt 0)

10+ Year Member



Let me start by saying that my example was just that, an example, and not the crux of the issue. There are undoubtedly circumstances where you can just navigate through the DOM tree using .childNodes and .parentNode (although this is complicated by the presence or absence of whitespace nodes), giving compact code, BUT this is not true all the time.

What I'm looking for is more what peoples' opinions are regarding the use of HTML DOM vs. core DOM - is it good programming practice? Or something you should only use for quick-and-dirty mockups? And why?

daveVk

3:18 am on Jan 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think your assesment is spot on, avoid html DOM, alternatives are not as convenient and clean, but hopefully better supported.