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

Testing JavaScript commands with the console

From: JavaScript: Enhancing the DOM

Video: Testing JavaScript commands with the console

In the last movie, we learned how to work with the developer tools. And how to activate the console. In this movie, we're going to focus just on the console. So the quickest way to get to the console is by hitting Command+Option+J. And I'm using a Mac right now. On a PC, Command is usually Ctrl, Option is usually Alt. So this would be Ctrl+Alt+J. It might be more convenient, though, to call the console within the elements panel. So, you can do that by clicking on the Elements panel and then hitting the Esc key. That toggles the console on and off. The DOM can be accessed by using selectors.

Testing JavaScript commands with the console

In the last movie, we learned how to work with the developer tools. And how to activate the console. In this movie, we're going to focus just on the console. So the quickest way to get to the console is by hitting Command+Option+J. And I'm using a Mac right now. On a PC, Command is usually Ctrl, Option is usually Alt. So this would be Ctrl+Alt+J. It might be more convenient, though, to call the console within the elements panel. So, you can do that by clicking on the Elements panel and then hitting the Esc key. That toggles the console on and off. The DOM can be accessed by using selectors.

So if I just type in document, it selects the entire DOM and I can click on it, just like I can with the Elements tab. The DOM has added completion, so as soon as I start typing doc, it's suggesting that I probably want the document, so I can hit Tab and then Return to execute that command. If you type in a period after document, then it's going to select from this list of options, all the methods and properties available to this element. So if I start typing b for the body, it shows me a shorter list, and I can use the arrow keys to move up and down this or just click on it with the mouse.

(SOUND) So document body takes you directly to the body element, of course document head takes you directly to the head. Now I can hit the Return key here to get to the end and then return again to execute that command. Once you're in, in node of a DOM tree, you can get to any of the subnotes using some of the normal JavaScript methods. So let's get the child notes of the body element. Body, and then type in child nodes, and remember Nodes is capital here.

Now this shows the list of all the nodes in the body in an array notation. I can also hit the up and down arrows to cycle through the list of current commands. You can also do that by hitting Ctrl+P or Ctrl+N. When you see an array notation like this it means you can access an element by typing in the array notation. So if I use brackets here and then select element 1, it's going to return the header. I'm going to hit up a few times so as to get this all entire list of nodes.

And notice that you can still click on these triangles to expand some of these elements. Now sometimes it's a little more useful to see this as a vertical directory lists. So you can type in something like dir(document.body, childNodes) and hit Return. And now it's a vertical list. You can of course use the normal JavaScript commands to access nodes as well, so we can do something like document.getElementByID.

Then, I'm going to type in ('main') and that gives me the element with an ID of main. Of course, you can also use the JavaScript querySelector method which is quite nice. Get to a document. And I hit the up arrow here and I'll do querySelector, and then type in what I would normally type in, in a CSS rule. So you can say pound main and that takes you to the same place. Now the console provides a shortcut for this querySelector method. You can get to it by hitting the dollar sign and then, in parentheses, just type in anything that you would type in as a selector for CSS. And say pound main (SOUND) and that takes us to same element. You can try other things like just the tag.

So I'll get to the footer tag like this, and maybe something more complicated. We'll try header and then find the navigation inside and ordered list element and then I'm going to use the greater than sign and then a list item element in there. So if hit Return, it gets the first item that match that rule. Now I know there are other list elements in there. So if I want all the elements that match the rule, and I'm going to hit up all the way to the left. So I'm hitting Cmd+Left to get to the left side.

And I'm going to hit the dollar sign to have two dollar signs. Then, Cmd+Right to get to the right side and hit Enter. And that gives me a list of all the elements that match that rule. The console is also going to store the last used element in a special variable called dollar sign, underscore. So if I type dollar underscore here, I get the last selection. Now you can get focused to an element in the DOM with the inspect method. So I'm going to grab this selector right here that got the first list item and I'm going to type in inspect, and I'm going to paste that in there. Now this is actually grabbing the element in the elements tab. Notice that the LI is selected right here.

So it didn't just get the element, it actually got it and selected it from the elements tab. You can ask the console to report unevents when they happens. For example, you can execute a monitor events command, and then type in a target. So I can say document.getElementByID (SOUND) and then main. And then put a comma and then click. So I'm going to look for click commands in the main element of this DOM. So I'm going to hit Enter and nothing looks like it's happening but I, if I happen to click anywhere within the main element and the DOM, notice that it's reporting that as a click event.

And I can get information about the event by opening these triangles up. Finally, if your console's getting a little bit messy, you can hit Ctrl+L to clear the console. There's really a lot of power in the Chrome developer tools. With complete access to and special methods for manipulating the DOM, it's really the best place to explore the inner structure of your documents.

Show transcript

This video is part of

Image for JavaScript: Enhancing the DOM
JavaScript: Enhancing the DOM

36 video lessons · 7578 viewers

Ray Villalobos
Author

 
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

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
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.


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 "Already a member? Log in

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

Your file was successfully uploaded.

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.