#HTML Form returning null

15 messages · Page 1 of 1 (latest)

bold minnow
#

I can't figure out why this is returning null. Any ideas?

<form id="dprForm">
  <label for="name">Name: </label>
  <input type="text" id="name"/><br><br>

  <label for="target">Target: </label>
  <input type="number" id="target"/><br><br>```
```ts
const form = document.querySelector('#dprForm') as HTMLFormElement;
form.addEventListener('submit', (event) => {
   event.preventDefault();

   const formData = new FormData(form);
   const name = formData.get('name');
   const target = formData.get('target');

   console.log(`${name} - ${target}`);
});```
fickle token
#

@bold minnow What part is null?

bold minnow
fickle token
#

What part?

bold minnow
fickle token
#

What part? Name or target?

bold minnow
#

It returns "null - null"

fickle token
#

Hard to say without context - try logging form see if it's the correct element. If so does the form actually have a field called "name" and a field called "target"?

bold minnow
fickle token
#

Pretty sure you need name="name" and "name="target" on your input elements.

bold minnow
fickle token
#

👆 MDN guides tend to be pretty good - if the tutorial you're following doesn't have name then yeah, it's probably not a good tutorial.

bold minnow
fickle token
#

FWIW that's a tutorial not a doc