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

JavaScript: Enhancing the DOM

Changing HTML attributes


From:

JavaScript: Enhancing the DOM

with Ray Villalobos

Video: Changing HTML attributes

Once you select an element, there are a few ways you access the attributes within that tag. The easiest is by using dot notation. So, for example you can access the source attribute of any image by using something like img.src. So usually, dot notation lets you just put the name of the attribute next to the node. These are read and write properties so you can change what an existing image is pointing to if you want. You just add it to a new value. You can even add attributes that don't currently exist in the dot. One thing you have to be careful with is that some attributes, like say the class attribute, are reserved JavaScript words. So, they have to be handled a little bit differently.
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

Changing HTML attributes

Once you select an element, there are a few ways you access the attributes within that tag. The easiest is by using dot notation. So, for example you can access the source attribute of any image by using something like img.src. So usually, dot notation lets you just put the name of the attribute next to the node. These are read and write properties so you can change what an existing image is pointing to if you want. You just add it to a new value. You can even add attributes that don't currently exist in the dot. One thing you have to be careful with is that some attributes, like say the class attribute, are reserved JavaScript words. So, they have to be handled a little bit differently.

So, let's take a look at this in an actual document. Just to get started, I want to target this element right here. It's an image inside a list. So, I'm going to pull up the developer tools. And hit Cmd+Option+I, that's Ctrl+Alt+I on the PC, and then I hit the Escape key. I want to get to this element, so I'm going to click the magnifying glass just to make it easy for me to visualize where that element is. And select the element right here. And gives me the breadcrumbs. And I know that I can get to this element by going to the artist, list, class. And then finding any image inside it. So, I'm going to set up a variable called my node and set that to document query selector, and in here find the class of artists list and the first image. Query selector will only get us the first image in this list. If I hit enter I'll type in my node.

And, sure enough, I've got just that first image. This node has two attributes. The src attribute and the alt attribute. So, let's see what we get when we type in mynode.src. We get the location of the image file. Since these are read and write attributes, we can change the image this file is pointing to. So, I'll try mynode.src and I'll use the equal sign to set it to something else. That replaces that image with another image.

That means that we can also add any attributes we want. We could for example, add an id name to this picture. So, I could say something like mynode.id, and then set it to, Select it. Now lets go ahead and pull up mynode and see what attributes it has. You can see that the selected attribute has been added right here. Just using the attribute name in dot notation is going to work for most attributes. But, there are some exceptions because, some attributes are the same as some of JavaScript reserved names. I mentioned class because class is a reserve word in JavaScript. So, we can't just say mynode.class equals myclass.

That doesn't work even though it looks like it worked, if we pulled again the node, we'll see that it didn't add that class. So, we have to use something called class name, so we can say my node dot class and then name. You can actually see it in the pop up list right here so you can pick it from that class name. Would then equal something else. Something like myclass. And now we've added that class name. Notice it's already showing up right here. But we can pull the mynode again and see that it's been added as an additional class.

So, labels are another good example of this issue. Let's go over to the register form, and if we pull up the magnifying glass and we access one of these labels right here. And I am going to change my variable, mynode to point to one of those labels. Now this should pick up the first label and notice that, that label has a for attribute. For in JavaScript is usually used to create loops, so we can use the value for here. In that case we have to use something like mynode.html and then capital For.

And lets check out mynode, and you can see now it has the new For value. The change a Boolean value like our radio button, you can use the keyword and then a true or false or a one or a zero. So lets try that. Let's go ahead and reset mynode again, and we've got a radio button down here. Let's go ahead and target it with the magnifying glass. So, we'll just ask the query selector to go fetch an input field with the type of radio.

Now you gotta be careful how you do this one, because I can't have single quotes everywhere. I need to combine double quotes on the outside with single quotes in the inside, so that it doesn't get confused. So, let's try mynode again, and we see that it has the right input field. And what we could do here is just say mynode.checked. And we can use the value true or false here. I'll set it to zero, which will be false. That means that that element is no longer automatically selected. So, let's check mynode.

And even though that value is checked, we were able to set it to be unchecked. (UNKNOWN) notation makes it easy to select elements but because you have to stay away from reserved words, sometimes its not as easy to remember. Also with that notation there's no way to delete attributes. We'll take a look at another way of handling that in the next movie.

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