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

JavaScript: Enhancing the DOM

Querying CSS to select elements


From:

JavaScript: Enhancing the DOM

with Ray Villalobos

Video: Querying CSS to select elements

Selecting elements can be a little bit of a pain with JavaScript. You can select things by ID, tag name, and on newer browsers, by class name. If you're familiar with CSS, one of the best ways of isolating elements is by using a newer function called querySelector. querySelector and its cousin, querySelectorAll let's you choose elements by using CSS-like notation. This let's you pick nodes through CSS selectors. That's super similar to the way that jQuery works. So, if you're familiar selecting things with jQuery, you'll be right at home with this.
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

Querying CSS to select elements

Selecting elements can be a little bit of a pain with JavaScript. You can select things by ID, tag name, and on newer browsers, by class name. If you're familiar with CSS, one of the best ways of isolating elements is by using a newer function called querySelector. querySelector and its cousin, querySelectorAll let's you choose elements by using CSS-like notation. This let's you pick nodes through CSS selectors. That's super similar to the way that jQuery works. So, if you're familiar selecting things with jQuery, you'll be right at home with this.

Now, this is not compatible with older browsers. But it does have a little bit better support than get elements by class name. Notice that it's supported by IE8. This is super easy to use, and probably the best way to select elements in HTML. So, I'm going to go to this HTML document and pull up the developer tools. So, I'm hitting Cmd+Option+I, that's Ctrl+Alt+I on a PC, then hitting the Esc key to pull up the console. Now, here, I'm going to type in documents.querySelector, and then put in article.

This is going to return the first element on my page that matches the selector in CSS notation. If you want to select every element that matches your selector, you can use the cousin function, querySelectorAll. Now, there's a difference between these two. This one returns an array like list with the results, and that's super important to remember because you access array-like stuff differently. So, you can do something like this, querySelector, and choose article, and then pick the childNodes, and that gives you all the children nodes of this article element, that happens to be this article element right here.

But you can't do querySelctorAll childNodes, that's because query selects are all returns on arrays. So, you have to specify which article you're referring to. Notice that when I come up here, I know that this is the first article, and this is the second article which is the featured artist section, right here. So, if I wanted to target this and get all the childNodes for this section, I have to add an array of one right here. Remember, arrays are zero index. So, element one is the seconds element, which may be what we want here. So, if I hit enter, now I can get all the featured artist children. Now, you can obviously get classes in your document like this. So, let's go to the artists page and get a list of all the artists. I'll click on artists right here, then I can do document querySelectorAll, in this case, because I want all of them.

And then, instead of typing in just the name of a tag, in quotes I can put in the name of a class, which would start with a period, and then something like artist. So, just like with CSS, you can also use things like type selectors. So, I'm going to go to the register page. And I'm going to do a document, querySelectorAll. And in here, I'm going to type in input, and then type=checkbox.

And it actually looks like I need to type in radio, because I don't have any check boxes. Let me change that to radio here. And that's going to select all of my radio elements within the form. I can also choose a descendant, so let's try something like document.querySelectorAll. And then, in here, I'll type in an ID of artists list, and then a descendant like a list item. So, I hit return, and I get all the list items within artists lists which are these elements over here.

I can also get a child, so let me add to this list item a child of image. And that gets me just the image tags. I can also ask for multiple items in the same list. I'm want to do document querySelectorAll, and then I'll ask for all the images. And after that, I'm also going to get. From the comingtoevents section, all the images as well. And that's going to give me all the images for the artists, as well as this phone right here, which is the image in the comment to event section.

So, by far, querySelector is the easiest way to get to elements. If you don't care about supporting browsers older than IE8, then you're in business. If you really want to support very old browsers, you may want to look into something called a Polyfill. A Polyfill is a way of supporting newer methods in older browsers. And here's a really good list of a bunch of HTML5 cross browser Polyfills. You can also go to this page where you can find a lot of other Polyfills. Now, if you really have to support other browsers, you should check out something like jQuery.

Which gives you a lot of other selectors. In newer browsers, you are safe with something like querySelector, and querySelectorAll.

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