-6

This is my html code:

<div class="input_cont">
<Input type="hidden" id="time" name="time">
</div>
<div class="input_cont">
<Input type="hidden" id="time" name="time">
</div>
<div class="input_cont">
<Input type="hidden" id="time" name="time">
</div>

And this is my javascript code:

const getTime = () {

const inpCont = document.querySelectorAll('.input_cont #time');

inpCont.forEach((fields) => { 
fields.querySelector('#time').value = '5:00:00 AM';
});

}

getTime();

But I am getting these errors:

    • Uncaught TypeError: cannot set property 'value' of null.
      • At NodeList that forEach ()
      • At getTime

Please help me, I am still learning and this is for my school project. Thank you.

Taplar
  • 24,788
  • 4
  • 22
  • 35

1 Answers1

1

Your arrow function was incorrectly formatted and you used the same ID more than once.

function getTime () {
  const inpCont = document.querySelectorAll('.input_cont [name="time"]');
  inpCont.forEach((fields) => { 
    fields.value = '5:00:00 AM';
  });
}

getTime();
<div class="input_cont">
<Input type="text" name="time">
</div>
<div class="input_cont">
<Input type="text" name="time">
</div>
<div class="input_cont">
<Input type="text" name="time">
</div>
Intervalia
  • 10,248
  • 2
  • 30
  • 60
  • 1
    This, plus the fact that `fields` in the OP's code already points to the `` elements, so the OP's `fields.querySelector('#time').value = '5:00:00 AM';` should be replaced by `fields.value = '5:00:00 AM';` like this answer did. – Anis R. Apr 30 '19 at 21:21
  • @AnisR. Thank you so much sir! I've been working with this error of almost 2 days and now its finally fixed! Thank you, once again. I need to study more. Hehe – Jasper Santiago Apr 30 '19 at 21:28