Mood Board Tutorial

Let's build a Todo-List!

Learning Objectives:

  • On-Submit Events

  • On-Click Events

  • DOM Element Creation

  • DOM Element Insertion

  • DOM Element Removal

Step 0: Set Up

Starter Code [download]

Create a folder called day_4 and create three files inside of it:

day_4

index.html

styles.css

index.js

Now, copy this paste this code into them:

Ask your Mood Board Questions on Parthean Community.

Step 1: Adding the Form

In our Mood Board, we have a form. Specifically, the form is at the top of the screen and it's where we input our image URLs.

To add a form, we use a <form> tag in HTML. Within forms, too, we can add specific inputs. These inputs range from simple text inputs, to dropdown inputs, checkboxes, and more. For our purposes, we just need to be able to input a URL and then submit it.

So within the body tag, insert the following code:

Go ahead and refresh your page -- what do you see?

If you want to make the form look a bit better, you can add the following css code to styles.css:

Ask your Mood Board Questions on Parthean Community.

Step 2: Form Submission

Now, we have to accept our form submission -- this is where Javascript comes in.

In order to accept inputs from an HTML form, we have to create an event-listener. This is Javascript code that listens for specific events. In this case, the event will be a form submission. Insert the following code into your index.js file:

This code is our event listener. So any time our form (with an id of #form_id ) is submitted, we'll run the code between the brackets.

Next, we need to read what the user typed into the text input. To do so, paste the following code into your file:

Now, we can read our user's input, but we're just alerting the url. Instead, we want to be using that url to insert an image into the screen. So let's call another function, called createNewImage to do so. Here's what your code should look like now:

Ask your Mood Board Questions on Parthean Community.

Step 3: Inserting Images to the Webpage

Now that we have our URLs, we need to insert images into the page.

To do so, we first want to create a new <img> object. Here's how:

In HTML, img object look like this: <img src="URL_TO_THE_IMAGE">. But so far, we've just created an <img> object with no attributes. So it looks like this: <img>.

To add that src attribute that we need, we can insert this code:

Now, our <img> object looks like this: <img src="URL_TO_THE_IMAGE">. Great! But we need to insert it onto the page.

When we want to insert our images onto the page, we have to have a specific location for them to go. As such, we must add the following <div> element in our HTML:

Now, let's add some Javascript to insert our images into this <div> object:

If you notice, the images look all wonky. to fix that, add the following to your css code:

Ask your Mood Board Questions on Parthean Community.

That's it!

You did it! You made a Mood Board application using DOM manipulation. Congrats!

Last updated

Was this helpful?