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

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

What is the DOM?

From: JavaScript Essential Training

Video: What is the DOM?

So we've been focused completely on the JavaScript language for these past few movies, and that is a great way to start with JavaScript. But of course it doesn't run in a vacuum. Our scripts will be attached to a web page, and it's the web page that comes first. They bring our script with them, not the other way around. So our page, whether a simple page or a complex one, is the canvas that we're painting on. It's the environment we're affecting. It is the world that we live in. And we must be able to reach from a JavaScript into these pages and cause these pages to reach back into our script.

What is the DOM?

So we've been focused completely on the JavaScript language for these past few movies, and that is a great way to start with JavaScript. But of course it doesn't run in a vacuum. Our scripts will be attached to a web page, and it's the web page that comes first. They bring our script with them, not the other way around. So our page, whether a simple page or a complex one, is the canvas that we're painting on. It's the environment we're affecting. It is the world that we live in. And we must be able to reach from a JavaScript into these pages and cause these pages to reach back into our script.

We do that by understanding the DOM, the Document Object Model. I consider knowing your way around the DOM to be the single most important skill for a JavaScript programmer to develop. But this is a term that's often a little tough to understand the first time you hear it, because it's kind of vague. People ask well, what is the DOM? Is it a language, is it part of JavaScript? What is it? Then you look it up on the web, and you find phrases like, "The Document Object Model is an application programming interface that defines logical structure of well-formed an XML and HTML documents," and it really doesn't help most people, but it's quite a simple idea at heart.

So let's take the DOM piece by piece. Well, what do I mean by document? Well, if I am in Microsoft Word, that might mean one thing, but we're not. For us, the document simply means the page, not the site, the web page; the web page is the document. But this document can be represented in different ways. Well, it already is. You know this already. After all, which one of these is the web page? Well, both of them. One is the browser view, one is the source code.

It's the same document; it can have a different representation. What we need to also understand is how JavaScript perceives the same document, same document, different representation. But if that's all we mean by document, then what's the object part of the Document Object Model? Well, when we talk about objects in programming languages, it's really easy to get wound up in deep semantic meaning, but we don't need to. We've already used objects in JavaScript. Dates are objects, arrays are objects. We've made our own objects. An object is just a thing, some thing, any thing that makes sense to treat as an individual piece, even if it contains other things.

For us, it really means the elements, the components, the individual pieces of this document. If I talk to a user, I can say, "Look at the headline, look at the bullet point list, look at the whole page, even look at the third letter in the second word," and they understand what I mean. If I talk to you as a web developer, I can say, "Now, look at the h1 tag, look at the unordered list part, look at the whole document." All of these things are objects and yes, sometimes they contain other objects. That's okay.

You have no problem understanding the unordered list piece contains individual list item pieces, and in the same way, what we'll do is we'll have a JavaScript term for every piece, for every object at whatever level that makes sense, from the entire document to the smallest piece, and that leads us to the idea of the model of Document Object Model. Now, what they mean here is model as in a business model or data model. You've seen flowcharts or database diagrams, the idea of taking something complex and making it abstract, making it simple.

But with a web page, we should be able to take any HTML and represent it as a tree structure. With the simplest of HTML here, it contains a head and a body. The head itself contains the title element, the body contains an h1 for a heading, a p for a paragraph, and an unordered list. The unordered list contains three list items. We're able with this diagram to represent the structure, the tree structure of this HTML document. We could even decide on words for these.

If we were drawing this up on a whiteboard, we could call each of these pieces nodes. We'll pick a p or a body or h1 and call them all nodes. We could even develop terms to describe relationships, like picking a node and asking, what's its parent? Does it have any child nodes? And really. here what the model means is, what do we call these things, and how do we describe the relationships between them? Because it's not going to do me any good if I make up my own names for these objects that conflict with your names for these objects, if I call a piece a node and you call it a doohickey.

So the model is simply a set of terms that we can agree on, a set of standards that we can use. So what we end up with is an agreed-upon set of terms that describe exactly how to interact with the pieces of a web page. That's a long phrase, so we call it the Document Object Model. It's not a language. It's an idea. It's a convention. But JavaScript agrees on this DOM, so it's available in JavaScript. It's an agreed-upon terminology that will let us describe and interact with any web page. Not one specific one, any one.

When I know the basic concepts of the DOM, I can write JavaScript that navigates around any page. I can say I want to get the text of the title. I want to get the second paragraph element. I want to get the third link in the menu and change how it's displaying, in this case hide it, or change the background color of all paragraphs, or get all the list item elements, or find an image and start to move it, change a link from its default behavior to actually performing a JavaScript function, or even creating new unordered lists, or creating paragraphs, or creating menus and inserting them into the page on the fly, and a lot more besides.

That's why we need to know the DOM. It is the way to reach into the page from our script and the way our page can reach into our script.

Show transcript

This video is part of

Image for JavaScript Essential Training
JavaScript Essential Training

56 video lessons · 99519 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

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.