Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- 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
Skill Level Intermediate
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.
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, 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.