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

patrick shangala
patrick shangala
6,217 Points

can't understand the question someone to help me out with this problem

how can i solve this problem ?

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

submitButton.addEventListener('click', () => {
  e.preventDefualt();
});
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>

2 Answers

Dave Harker
PRO
Dave Harker
Pro Student 15,507 Points

Hi Patrick Shangala ,

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

/***
https://developer.mozilla.org/en-US/docs/Web/Events/submit 
Note that submit is fired only on the form element, not the button or submit input. (Forms are submitted, not buttons.)

So you'll need for change that addEventListener to listen on the 'form' not the 'submitButton'
You'll also need to listen for 'submit' not a 'click'
And lastly, to use the event (e) in your function, you'll need to pass it through 
***/  

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

I could write out the code but I hope that helps you more.
You're getting there! Keep up the effort :smile:

Happy coding,
Dave :dizzy:

patrick shangala
patrick shangala
6,217 Points

not all still can't figure it out ..can you please just show me the code or you also don't know

Dave Harker
Dave Harker
Pro Student 15,507 Points
const form = document.querySelector('form');
const submitButton = form.querySelector('[type=Submit]');

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

Good luck ... you're going to need it.