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

JavaScript: Enhancing the DOM

Communicating with the console through JavaScript


From:

JavaScript: Enhancing the DOM

with Ray Villalobos

Video: Communicating with the console through JavaScript

In the last movie, we saw how we can issue commands to HTML page from within the browsers console. In this movie, we're going to take a look at how to communicate with the console from within a JavaScript document. So I'm going to go to the HTML document for this page. And, at the very bottom, I'm going to add a script tag. And that script tag is going to call a document called myscript.js. Now, that document is inside an underscore folder and inside a JavaScript folder, and it's called myscript.js. So, I'll save this, and then I can close my index HTML document and work on the myscript.js 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

Communicating with the console through JavaScript

In the last movie, we saw how we can issue commands to HTML page from within the browsers console. In this movie, we're going to take a look at how to communicate with the console from within a JavaScript document. So I'm going to go to the HTML document for this page. And, at the very bottom, I'm going to add a script tag. And that script tag is going to call a document called myscript.js. Now, that document is inside an underscore folder and inside a JavaScript folder, and it's called myscript.js. So, I'll save this, and then I can close my index HTML document and work on the myscript.js file.

In the early days of JavaScript, the most common way of debugging your software was through the alert message. It looks something like this and let me go ahead and save this. Switch back to my HTML page and just refresh the browser. You can see the alert comes right up. That really gets your attention. But it's not very useful. Especially if you're testing for things that happen often, like for example, a loop. The more modern way is to send something to the console, so that we can monitor things as they happen. So, you could use the console.log, command.

And it does sort of the same thing, but it doesn't pull up an alert. So let's switch back to the document, let's go ahead and close our alert, refresh, and nothing pops up, but in the console, you can see the message that we typed in. Of course, that doesn't seem that useful. But you can pass along any element into the console from the DOM. So instead of putting in a message, we can type in document.querySelector and pass it along any DOM element, so we'll pass along the element with the ID of main and save this. Come over here, and refresh and now we have that DOM element, and we can open that up just like if it was in the Elements tab. There's actually a lot of other commands you can send to the console. So for example, you can specify that this information be given to you in a directory format.

If I say dir, I'll hit Save, come back here, refresh and we are getting a different type of message that shows you all the methods and properties for that element. You can also specify a type of formatting for your message by using different methods. So instead of doing dir, you can do console info and I'll just type in here some message, save it, and let me refresh. You'll see that it's just sort of like same thing as the console log command.

But let's try a console.warn message. So if we come back here, and refresh, we'll see that you get this little warning sign right here, plus the warning sign down here. You're sort of forcing a warning into the console. And also, of course, through an error instead of a warning and that should really get somebody's attention. I'll refresh the page and see that it's now in red and it has this little error message right here. You can group a series of console commands into sections by using console groups.

So let me try that. We'll do console.group. And we'll create different groups here. So we'll call this one Page Links. And we'll issue a console.dir command here. So there is just a forced directory structure so that it shows you the messages and properties, and then we'll pass along a document.querySelectorAll. It will ask for all the anchor tags. And then we'll issue a console.groupEnd and that closes this group. So then we can start another group, we'll do a console, and then do groupCollapsed message here.

It's very similar, we'll show you the formatting in a minute. And we'll call this one Paragraphs, then we'll ask for a console directory command. We're asking for document.querySelectorAll, and this time, we'll ask for all the paragraphs, and then we'll issue a console.groupEnd... Save that and switch over to the document, and refresh, and I can see that we have a couple of messages. This one's already expanded, so that's what the console.group command does. The console.groupCollapse actually collapses it by default so this one shows you.

A list of nodes right here, and this one shows you pretty much the same thing except that it's collapsed by default. So you can open this up and take a look at the elements within. If you're trying to determine how long something takes in your JavaScript document, you can ask the consult to log, how long something took. So let's take a look at that. I'm going to go ahead and start a timer by saying console.time. Then in here, we'll issue a label called BigLoop. And then we'll just execute some sort of generic loop here.

We'll ask for something to be from one to a million, just so that we can have some time difference there. And, we don't really want anything to happen in here. But then we'll execute a console.timeEnd, and we'll pass it along the same label that we passed along up there. They have to match, so we'll say BigLoop. Make sure you capitalize properly. And then, I'm going to save this and come back into my document and refresh the page. Notice that it's telling me that this label is taking 3.72 milliseconds.

So you can execute as many of these timers as you want within your JavaScript. Just make sure that the labels match perfectly. Another important debugging command is called the assertion. So with an assertion, you can test for a condition. If the condition executes as false, then it'll output something to the console. So let's try that. So let's do a console.assert, and in here, we'll just test something in the document. So we'll do document.querySelectorAll, and we're going to check to see if there are two navigational links here, so nav ol>li.

So, in the navigation, if the length of the elements within the selector is equal to 2, then we say here, Sorry, there's only two menu items. Let me go ahead and put this in double quotes here, because single quote here is going to mess things up so, there, something like that. So, is this executes to false? Then, it'll output this line right here, which I know that my document has three lengths, so this should execute as false, and we should see the error. So I'll save this, come back here, refresh, and you can see that it says, Assertion failed, sorry, there's only two menu item's, because there's three right up here.

So if we come back and we say, if the length of those is equal to 3, then we won't get that assertion error. So the old-fashion alert message and even console log have a lot of new cousins. A lot of these methods are also available in other browsers. So there's definitely more options for log and data from your scripts into the console.

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

Share a link to this course
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.

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
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



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.

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.