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

JavaScript: Enhancing the DOM

Adding an image


From:

JavaScript: Enhancing the DOM

with Ray Villalobos

Video: Adding an image

In the last movie we created an overlay and placed it on top of our window. What we want to do now is add the image on top of that overlay. To do that we're going to need to figure out where our large image is. Now if you take a look at my project you can see that I have a bunch of images here. When I click on one you see the overlay, but I need the big image right here. So my images have been very carefully saved in the same folder as the other images. So you could see here, here's the artwork section. So if I got a thumbnail called Baird Bellingham_tn.jpeg the large version of the image is the same name without the underscore thumbnail.
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

Adding an image

In the last movie we created an overlay and placed it on top of our window. What we want to do now is add the image on top of that overlay. To do that we're going to need to figure out where our large image is. Now if you take a look at my project you can see that I have a bunch of images here. When I click on one you see the overlay, but I need the big image right here. So my images have been very carefully saved in the same folder as the other images. So you could see here, here's the artwork section. So if I got a thumbnail called Baird Bellingham_tn.jpeg the large version of the image is the same name without the underscore thumbnail.

First I need to find out he name of the item that I clicked on. That is stored in the event target. You can see right here that we were able to figure out the tag name of the image by querying the event target. And then the property tag name. So right over here. I want to create a new variable. I'm going to call it Image source. And I'm going to set it to the event target, and the source of that event target. So let's go ahead and just output that to the console, so we can see it.

Console, log and then I'll output the e.target. Switch over to our project, I'm going to refresh and I'll click on one of these images and then I will show the console. And notice that whatever image I click on, it's going to show me the element and the source attribute is going to have the location of that image. It's going to have the location of the thumbnail, so we'll need to worry about that in a minute. So we refresh, so we see that if I click on another image, it outputs the target, which is the note that has that image. Let's go back into our code.

Let me go ahead and delete this console log. So I'm storing the source or the location of the current target into this variable I called Image source. So I'm going to create another variable called Large image. And that's going to be a new element that I'm going to create, so I'll do a document.createElement, and a create a new image element (SOUND). I'm going to give this an ID, so largeImage.id is going to be largeImage. And I'm going to set the source of this new image element to be equal to my old image source and I want to do a sub string method here. The substring method will take a string and allow me to truncate part of that string.

So what I want to do here is say, 0, start at the 0 position, and then find the image source, and take the length of that -7. So that is, whatever the image name is, without the _tn.jpg. And then just add the word, jpg, to it (SOUND). That way, we'll get the location of the high-resolution image. Now we'll modify the style of this image. So we'll set the display to block.

Just like we would in css, and then we will set the position to absolute. Finally I'll pin this new image to the overlay. So I'll save my overlay dot appenchild and then I'll pass it this large image. So I'm going to go ahead and save this. Come over here, I'll refresh the page and then click on one of these images. You can see the big image comes in, it's not centered, it's too big, but at least it's coming in properly.

So if you click on any of these you should be able to see the large version of the image. And that's all we want to do in this 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