New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

JavaScript: Enhancing the DOM
Illustration by

Understanding nodeType, nodeName, and nodeValue


From:

JavaScript: Enhancing the DOM

with Ray Villalobos

Video: Understanding nodeType, nodeName, and nodeValue

Every DOM element has a few important properties you can use to identify the node. So we're going to take a look at some of those properties in this movie. So, for example, the nodeType property gives you a numerical value of a node. The nodeName property gives you the name of the node, usually the tagName. So for example an anchor tag would have a type of A. Node.atrributes is a how you can access attributes inside a node. So if you were inside an anchor tag you could ask for a list of attributes and get the href attribute. Also the node value let's you access an element inside a node. So let's take a look at these in an actual document. So here we are in our sample file.
Expand all | Collapse all
  1. 2m 36s
    1. Welcome
      59s
    2. What you should know before watching this course
      50s
    3. Using the exercise files
      47s
  2. 24m 33s
    1. What is the Document Object Model (DOM)?
      3m 2s
    2. Navigating the DOM with developer tools
      8m 10s
    3. Testing JavaScript commands with the console
      5m 50s
    4. Communicating with the console through JavaScript
      7m 31s
  3. 31m 9s
    1. Selecting elements with getElementById
      4m 10s
    2. Choosing elements by HTML tag
      3m 20s
    3. Isolating elements by class name
      3m 12s
    4. Querying CSS to select elements
      4m 54s
    5. Working with named form elements
      3m 39s
    6. Understanding nodeType, nodeName, and nodeValue
      4m 30s
    7. Traversing up and down DOM nodes
      4m 40s
    8. Targeting node elements
      2m 44s
  4. 22m 25s
    1. Changing HTML attributes
      5m 25s
    2. Working with restricted attributes
      2m 49s
    3. Detecting data attributes
      3m 29s
    4. Controlling classes with the HTML5 classList
      3m 21s
    5. Targeting the attributes property
      1m 24s
    6. Using text content modifiers
      3m 42s
    7. Modifying elements as text
      2m 15s
  5. 14m 57s
    1. Creating and appending nodes
      4m 27s
    2. Controlling node insertions with insertBefore
      3m 17s
    3. Cloning and removing nodes
      4m 41s
    4. Replacing existing nodes
      2m 32s
  6. 26m 14s
    1. What we'll build
      2m 16s
    2. Adding a bubbling event listener
      4m 11s
    3. Creating and styling an overlay with JavaScript
      4m 39s
    4. Adding an image
      3m 48s
    5. Resizing images in the DOM
      2m 59s
    6. Centering an image
      2m 36s
    7. Handling clicks
      1m 29s
    8. Adjusting for scrolling
      1m 36s
    9. Detecting and adjusting for a window resize
      2m 40s
  7. 1m 49s
    1. Next steps
      1m 49s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
JavaScript: Enhancing the DOM
2h 3m Intermediate Jun 10, 2013

Viewers: in countries Watching now:

The Document Object Model (DOM) is at the core of every HTML page. In order to develop dynamic HTML pages, a front-end developer needs to understand how JavaScript connects to and controls the DOM, allowing you to create, modify, delete, and edit existing page content. This course focuses on helping you understand the DOM elements, and shows the different ways JavaScript gives you access to them and makes it easier to work with the DOM. Author Ray Villalobos covers navigating the DOM, selecting elements, modifying HTML attributes, editing nodes, and much more.

Topics include:
  • What is the DOM?
  • Choosing and isolating elements
  • Traversing up and down DOM nodes
  • Changing HTML attributes
  • Modifying elements as text
  • Creating and appending nodes
  • Cloning and removing nodes
  • Adding a bubbling event listener
  • Adding and resizing images
  • Handling clicks
Subjects:
Developer Web Web Design Web Development
Software:
HTML JavaScript
Author:
Ray Villalobos

Understanding nodeType, nodeName, and nodeValue

Every DOM element has a few important properties you can use to identify the node. So we're going to take a look at some of those properties in this movie. So, for example, the nodeType property gives you a numerical value of a node. The nodeName property gives you the name of the node, usually the tagName. So for example an anchor tag would have a type of A. Node.atrributes is a how you can access attributes inside a node. So if you were inside an anchor tag you could ask for a list of attributes and get the href attribute. Also the node value let's you access an element inside a node. So let's take a look at these in an actual document. So here we are in our sample file.

I'm going to hit Cmd+Option+I, Ctrl+Alt+I on the PC and then hit the Esc key to pull up the console. So I'll start out by creating a variable called myNode, and then assigning it an element in our page. In this case, I'm going to choose the last link in our navigation menu, which is right here, so we'll type in document.querySelectorAll. Find the navigation list item anchor tags. And then choose the last element, happens to be element number 4.

So, once that's in a variable, I can type that in. And note that I have to write element. Now, let's type in myNode.nodeType. So that it's returning a value of one. You get a one because this is a normal element to node. Each node in the dome has different node type. It can be anything from a element to a text attribute or other node. You can find out more about node types on this page right here.

If you scroll down here to the bottom you can see the type of node types by numbers. So that notices that a normal element node is type number one. You have attribute nodes, text nodes, and then a bunch of other sort of weird nodes. You're definitely going to use element nodes, attribute nodes, and text nodes. You can also find out what a node is, by asking for the node name. So this is telling you that this is an A element, or an anchor tag. Now you can get to any attribute within this node by using the attributes array. So I can say myNodes.attributes. And now it's giving you a list of the attributes inside that node. Notice that I only have one attribute here called href, so it's only returning one item which is right here.

So to get to that, we would type in myNode attributes and then the 0 element. Now we can find out the node type of this attribute as well. So we can say something like this and then say nodeType and notice that this is type 2. And if you remember from this page, type 2 is an attribute node. You can also get the node name, and that returns the fact that this is the atra function. So sometimes you want to get to all the properties or attribute within a node, and you can access them through this array.

Now if you want to, you can go one level deeper and get to the text of the link. So I'm going to say, myNode first child. Now the first shell seems to be the text for this link, called register. But let's make sure and try typing in the nodeType. Now this is type 3 so we'll go back into our list of nodes here and note that this is a textNode, it's not text, but a textNode. So you may think that you can do something like mynode.firstChild and set it something like registration to change the text inside that first link.

That doesn't work. That's because the first child of the node is a node of type of text node. It's not a text element so its not real text so its just another node. Like another element inside our DOM tree. To actually get to the text we have to type in myNode.firstChild and then use the nodeValue attribute. Then we can set that something like registration and that changes our link right here. What you have to remember is that technically everything in the dom is a node including text elements. So to get to the text of a node we have to use the first child and then we have to access the value by going to the node value property. So as you learn to manage and control your nodes, you'll need to get familiar with these different properties.

They'll let you not only access but also manipulate your DOM nodes.

There are currently no FAQs about JavaScript: Enhancing the DOM.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript: Enhancing the DOM.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.