Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Adding and Removing Names Using the submit Event

shobha gunupuru
shobha gunupuru
1,137 Points

Alter the event handler on line 4 to run when the form is submitted, not just when the submit button has been clicked. L

how to solve this

app.js
const form = document.querySelector('form');
const submitButton = form.querySelector('[type=Submit]');

submitButton.addEventListener('click', (e) => {

  form.preventDefault();

});
index.html
<!DOCTYPE html>
<html>
<head>
  <title>Submit Event</title>
</head>
<body>
<form>
  <label>Name:</label>
  <input type="text" name="name">
  <input type="Submit" name="submit" value="Submit">
</form>
  <script src="app.js"></script>
</body>
</html>

5 Answers

Cosimo Scarpa
Cosimo Scarpa
14,047 Points

Need to be

const form = document.querySelector('form');
const submitButton = form.querySelector('[type=Submit]');

form.addEventListener('submit', () => {

});

You're adding the EventListener to the form element instead of the submit button, also you need submit and not click.

Steven Parker
Steven Parker
229,744 Points

You need to make two changes to how this event listener is attached.

First, you would want to attach the listener to the form itself instead of the button. Then, you would want to change the event type (the first argument to addEventListener) from "click" to "submit".

Also, it looks like you already have code added for task 2. Did you pass task 1 already somehow?

But your task 2 code isn't quite right. You have the right idea in calling preventDefault, but it should be applied to the event object (e) instead of the form.

const form = document.querySelector('form'); const submitButton = form.querySelector('[type=Submit]');

form.addEventListener('submit', (e) => { e.preventDefault();

});

Steven Parker
Steven Parker
229,744 Points

You forgot to format it, but yes, that's what it would look like after you apply the hints I gave.

When posting code, always format using the instructions in the Markdown Cheatsheet pop-up, found below the "Add an Answer" area :arrow_heading_down:

vikram 11023
vikram 11023
12,043 Points

Alter the event handler on line 4 to run when the form is submitted, not just when the submit button has been clicked. Leave the callback function empty for now.

const form = document.querySelector('form'); const submitButton = form.querySelector('[type=Submit]');

form.addEventListener('submit', (e) => {
const text = input.value; input.value = ''; });

I could be totally wrong here but I'm thinking it's something like this:

form.addEventListener(submit, (e) => {

form.preventDefault(); // this line is preventing submit

});

I just checked the challenge and that worked fine. I did leave out the form.preventDefault(); since that wasn't needed.