This assignment will teach you the following:
- HTML Forms
- DOM element selection
- DOM traversal
- DOM manipulation
- Event handling
Merge your pull request from the previous lesson (if you haven't already):
Fetch the updated instructions from the base repository:
Note: you may receive a conflict if you've made changes to the README or other instructions
Checkout your main branch and pull changes:
git checkout main
git pull
Create a new local branch to work on separate from the main branch:
git checkout -b lesson-4-3
Now, open the project directory in your code editor and continue to the next section.
- Open your
index.htmlfile - Above the
<footer>element, add an empty<section>element - Inside the new
<section>element, create a level-two heading that says "Leave a Message" - After the heading, create an HTML
<form>element with anameattribute that equals "leave_message" - Inside the
<form>element, add the following:<input>element with attributes:type"text",name"name", andrequiredtrue<input>element with attributes:type"email",name"email", andrequiredtrue<textarea>element with attributes:name"message" andrequiredtrue<button>element that says "Submit" and hastypeattribute equal to "submit"- Each form field should also have a corresponding
<label>element - (Optional) Use
<br>elements to stack the form fields
- Save and refresh your browser
- After the
<section>element from the previous step, create a new<section>element with anidof "messages" - Inside that element, create a level-two heading that says "Messages"
- After the heading, add an empty unordered list (
<ul>) element - Save and refresh your browser
- Open your
index.jsfile and start at the bottom - Using "DOM Selection", select the "leave_message" form by
nameattribute and store it in a variable namedmessageForm - Add an event listener to the
messageFormelement that handles the "submit" event- hint:
addEventListenermethod
- hint:
- Inside the callback function for your event listener, create a new variable for each of the three form fields and retrieve the value from the event
- hint:
event.targetis the form,event.target.nameis the first input element
- hint:
- Inside the callback function for your event listener, add a
console.logstatement to log the three variables you created in the previous step - Save and refresh your browser
- Fill out the HTML form in your browser and hit "Submit"
Note: at this point, you should notice that the browser is refreshing automatically when you submit your form which is not the desired behavior
- Inside the callback function, above the other code you just wrote, add a new line to prevent the default refreshing behavior of the "submit" event
- hint:
preventDefaultmethod
- hint:
- Save and refresh your browser
- Fill out the HTML form in your browser and hit "Submit"
- You should see that the page does not refresh and your values are logged in the console
Note: at this point, you should notice that the form is submitting properly but the form fields are not reset after submit
- Inside the callback function, on the very last line, add a new line of code to clear the form
- hint:
resetmethod
- hint:
- Save and refresh your browser
- Open
index.jsand start inside the event listener callback function on the line above where you reset the form - Using "DOM Selection", select the #messages section by
idand store it in a variable namedmessageSection - Using "DOM Selection", query the
messageSection(instead of the entiredocument) to find the<ul>element and store it in a variable namedmessageList - Create a new list item (
li) element and store it in a variable namednewMessage - On the next line, set the inner HTML of your
newMessageelement with the following information:<a>element that displays the "name" and links to the "email" (hint: use themailto:prefix)<span>element that displays the "message"
- Create a new
<button>element and store it in a variable namedremoveButton- Set the inner text to "remove"
- Set the
typeattribute to "button" - Add an event listener to the
removeButtonelement that handles the "click" event- Inside the callback function, find the button's parent element using DOM Traversal (hint:
parentNodeproperty) and store it in a variable namedentry - Remove the
entryelement from the DOM (hint:removemethod)
- Inside the callback function, find the button's parent element using DOM Traversal (hint:
- Append the
removeButtonto thenewMessageelement- hint:
appendChildmethod
- hint:
- Append the
newMessageto themessageListelement - Save and refresh your browser
These tasks are entirely optional, but if you'd like a challenge then do your best to complete each item.
- (Optional) Hide the #messages section when the list is empty
- (Optional) Create an "edit" button for each message entry that allows the user to input a new/modified message
Check the status of your local repository to double-check the changes you made:
git status
Stage the file(s) that you edited:
git add .
Check the status again and notice that the changes from before are now staged:
git status
Create a commit for the changes you made and add a message describing the changes you made:
Note: Replace
<message>with your message
git commit -m "<message>"
Push your commit to the remote repository (visible in GitHub):
git push
Check the log to make sure your commit has been published:
git log --oneline
Create a pull request and submit:
Created by Code the Dream

