N_G
N_G

Reputation: 15

How can i get value using data.get() function

I am trying to get the value of a textarea that I have append into a fieldset of my form. To do so, when I submit my form, the following code is triggered:

function handleSubmit(e) {
  e.preventDefault();
  console.log(document.getElementById('Observation_1').value);
  const data = new FormData(e.target);
  const value = data.get('Observation_1');
  console.log(value);
}
const form = document.querySelector('form');
form.addEventListener('submit', handleSubmit);
<form>
  <textarea id="Observation_1"></textarea><br>
  <button type="submit">Test</button>
</form>

however, my console.log (value); returns me null, while my console.log(document.getElementById('Observation_1').value); return me the content of my textarea.

Can someone explain me why i have a null return and help me to fix my code so i can get the actual content of my textarea please.

Upvotes: 1

Views: 484

Answers (1)

Endless
Endless

Reputation: 37806

You probably don't have a name="Observation_1" attribute on the textarea.

Upvotes: 3

Related Questions