2

JavaScript and CSS both use its own DOM tree when traversing through HTML elements.

In JavaScript, we can use its own DOM traversal methods such as

element.parentNode
element.nextSibling

However, this way is very unintuitive because JavaScript's DOM Tree contains things other than the HTML elements which can be easy to mess up for a developer.

i.e.

<div>
     <p>
     </p>
</div>

JavaScript's DOM Tree has <div> as the parent and 3 children:

text node: \n\t

element node: <p>

text node: \n

This becomes increasingly more difficult to keep track as the HTML document structure increases in complexity because not all HTML elements have a newline before and after among other things.

Thankfully, JavaScript allows us to use CSS's DOM traversal using:

element.querySelector("CSSselectorHere")

Since CSS's DOM Tree only contains the HTML elements, that makes it much easier to implement.

The case only I could think of where JavaScript's DOM Tree would be advantageous is if you are trying to color the text "cdf" red in the following:

In HTML:

<p> abc <a href="...">link</a> cdf </p>

In JavaScript:

pElementVariable.firstChild.nextSibling.nextSibling.classList.add("CSSclassRed");

However, wouldn't a better HTML practice be to just enclose the unique text like so?

<p> abc <a href="...">link</a> <span>cdf</span> </p>

Thus, styling the span using CSS's DOM traversal is possible. (assuming we're using traversal methods only, no ids)

If so, does .querySelector, an enabler in JavaScript for CSS's DOM traversal, make JavaScript's own built-in DOM traversal methods obsolete?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
NoName
  • 9,824
  • 5
  • 32
  • 52
  • Is this a homework problem? I don't really see what the question is here... it seems more like a an opinion. – scottheckel Oct 18 '16 at 04:00
  • https://drafts.csswg.org/selectors-4/#profiles – Daniel Lizik Oct 18 '16 at 04:06
  • "However, wouldn't a better HTML practice be to just enclose the unique text like so?" No because in HTML using non-semantic elements isn't considered good practice. – BoltClock Oct 18 '16 at 04:08
  • Styling the non enclosed text doesn't work. classList is undefined. I tried https://jsfiddle.net/x6xd32bx/ – ariel Oct 18 '16 at 04:20
  • 1
    @Hexxagonal no, I just read a chapter about DOM and I'm wondering if I can just memorize .querySelector if there are no edge cases where it doesn't work. I explained a lot so people know where I'm coming from. – NoName Oct 18 '16 at 04:22
  • 1
    @NoName For the most part, yeah go for it. It gives you much cleaner code. That's the reason jQuery took off so many years ago, and querySelector has decent support in modern browsers. – scottheckel Oct 18 '16 at 04:27
  • I think @Daniel_L is suggesting that while the DOM uses the static profile, CSS uses the dynamic profile and therefore has a cut-down version of the selectors-4 standard. But considering selector profiles aren't even implemented yet, and even if it was, we're talking about the DOM anyway, I'm not sure how that is supposed to be relevant... – BoltClock Oct 18 '16 at 04:29
  • But it's worth clarifying that selectors-4 makes it a point to distinguish between "Selectors" ("CSS selectors") and CSS. CSS itself doesn't do any form of DOM traversal. All it does is match elements with selectors and apply styles accordingly. The querySelector family of methods is all part of the DOM. – BoltClock Oct 18 '16 at 04:32
  • @BoltClock Fine... In HTML:

    abc

    link

    cdf

    In CSS: p{display:inline};
    – NoName Oct 18 '16 at 04:32
  • 1
    @NoName: There's nothing stopping you from using container elements such as span and div if necessary for the purposes of JavaScript and CSS, since they have no semantic value and therefore don't change the meaning of your document, but in the context of HTML it's just plain unnecessary. – BoltClock Oct 18 '16 at 04:34
  • @BoltClock Actually, I was hinting my original html snippet at the end was linked to a JavaScript that would find the added span with .querySelector. If so, it would be semantically correct? – NoName Oct 18 '16 at 04:42
  • 1
    @NoName: You can add the span to the HTML after the fact. But it's not semantically *incorrect* to just have it in the HTML. I was probably nitpicking on the "better practice" side of things... – BoltClock Oct 18 '16 at 04:46

1 Answers1

6

No. CSS is more limited, because it can only select elements (and pseudo-elements, but not through querySelector, but that might change).

Using the DOM you can traverse arbitrary nodes in the tree. That's more powerful. And if you want to restrict to elements, you can:

node.childNodes; // All child nodes
parentNode.children; // Only element child nodes

node.firstChild; // First node child
parentNode.firstElementChild; // First element child

node.lastChild; // Last node child
parentNode.lastElementChild; // Last element child

node.previousSibling; // The previous sibling node
nonDoctypeChildNode.previousElementSibling; // The previous sibling element

node.nextSibling; // The next sibling node
nonDoctypeChildNode.nextElementSibling; // The next sibling element

node.parentNode; // The parent node
node.parentElement; // The parent node, only if it's an element

So you don't need CSS APIs for things like that. And in particular you shouldn't modify your HTML to be able to use CSS APIs.

Oriol
  • 274,082
  • 63
  • 437
  • 513