This lesson covers HTML text nodes, their manipulation with JavaScript, and the distinction between text nodes and element children in the context of a complete frontend developer textbook for beginners.
Abstract
The provided content is an excerpt from "A Complete Frontend Developer Textbook for Beginners (2023 Edition)" focusing on Chapter 16, which delves into the concept of HTML text nodes. The lesson explains how to interact with text nodes using JavaScript, specifically through properties like textContent and methods to navigate between siblings and children in the DOM tree. It emphasizes the difference between text nodes, which hold plain text, and element nodes, which are represented by HTML tags. The chapter also includes practical examples, code snippets, and a concept quiz to reinforce learning, along with interactive coding exercises and syntax flashcards for hands-on practice. The lesson aims to solidify the understanding of the Document Object Model (DOM) and prepare students for more advanced topics in frontend development.
Opinions
The lesson emphasizes the importance of understanding text nodes for frontend development, considering them crucial for manipulating the DOM effectively.
It suggests that mastering text nodes is essential for readying oneself for the upcoming unit on the DOM Tree.
The author provides a rationale for the existence of text nodes, explaining their role in storing plain text within the DOM.
The lesson encourages the use of specific JavaScript properties and methods to select and manipulate text content efficiently, avoiding the selection of invisible text nodes when necessary.
It posits that the distinction between text nodes and element nodes is fundamental for accurate DOM manipulation.
The inclusion of a concept quiz, interactive coding exercises, and syntax flashcards indicates the lesson's commitment to interactive and engaging learning experiences.
The author believes in the practical application of knowledge, as evidenced by the exercises provided to apply the concepts learned in real-world scenarios.
The lesson is part of a structured curriculum, indicating a progressive learning path for beginners in frontend development.
Chapter 16: HTML Text Nodes in JavaScript
A Complete Frontend Developer Textbook for Beginners (2023 Edition)
This lesson begins with this Codepen. Code along with me to increase retention!
Lecture
Starting with this lesson, we begin to learn much more about the contents of tags. To start, let’s explore tags that were created specifically to hold plain text.
In Lesson 6, we were lightly introduced to some very common content tags: h1 through h6, img, a, button, and of course, p. Let’s begin by studying the paragraphtag (p) in more detail.
textContent in JavaScript
To select the firstp tag using JavaScript, we can write:
document.querySelector(“p:first-child”)
Remember, we can use anyCSS selector inside these quotes.
As you can see, these shortcuts let us ignore the textNodes if we so choose because, most of the time, these textNodes are just newlines and indentations to help make our HTMLcodeclean and readable.
In fact, it is also possible to select only the elementchildren of main like this (the 2 p tags):
To recap, HTML tags become nodes in the DOM Tree. Plain texts are unusual because they are nameless tags. Nevertheless, they still exist as nodes in the DOM Tree. To easily distinguish between namedtags versus unnamedones, named tags are additionally referred to as elementnodes.
Summary
In the first part of this lesson, we explored a special HTML tag called a textNode.
It is special because it is a namelesstag. Nevertheless, it still exists in the DOM Tree and is responsible for storing all the plain text we write in the HTML document.
Taking the time to master textNodes will prepare you for Unit 3 where we will dive deep into the DOM Tree.
Concept Quiz
Take my Programming Concept Quiz to check your understanding! For every correct choice, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Sample Quiz Questions for Lesson 16:
Question 1:
Which HTML element is used to storeplain text?
p
span
textNode
Question 2:
textNodes have a tagname.
True
False
Question 3:
textNodes exist in the DOM Tree.
True
False
Question 4:
HTML:
<p>Hello World</p>
Which code will select the contents of the p tag?
document.querySelector(“p”).text
document.querySelector(“p”).content
document.querySelector(“p”).textContent
Question 5:
HTML:
<p></p>
This p tag has a textNode.
True
False
Question 6:
HTML:
<p></p>
What is the textContent of p?
""
" "
null
Question 7:
HTML:
<p></p>
What is the textContent of p?
""
" "
"\n"
Question 8:
HTML:
<p>Hello World</p>
Which code will NOT select the contents of the p tag?
Check out my Interactive Coding Exercises to put to practice what you have learned! There, you will also find interactive hints to help you understand each line of code. Likewise, for every correct solution, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Review what you have learned by playing my Syntax Flashcard Game! These flashcards are designed to help you commit to memory all the newcodesyntaxes you learned in this lesson. Likewise, for every correct answer, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Now that you have mastered textNodes, you’re ready to learn about all the various content tags that are intended to contain textNodes. There are many besides the p tag! In the next lesson, we will go over all the most popular and some not-so-popular as well. Moreover, we will also learn about some interestingCSS styles that are intended just for these content tags! Like the sectioningtags, mastering how and when to use the correctcontent tags will make your webpage search-enginefriendly and screen-reader friendly!
When you use my referral link above 👆 to become a Medium member, all proceeds will be donated towards the construction of the Silicon Wat Campus for children in Ukraine and Cambodia ❤️