-2

So i'm trying to just access the value from my date input

let object = document.getElementById('tripdateinput').value;
console.log(object);
<input autocomplete="off" id="tripdateinput" type="date" name="tripdate" required>

but when i try to console.log(object) it returns as empty. This is also only happening with my date inputs my normal text inputs are working.

Erfan Bahramali
  • 392
  • 3
  • 13

1 Answers1

3

Add this script to get your selected date value:

document.getElementById('tripdateinput').addEventListener("change", function(event) {
  console.log(event.target.value);
});
<input autocomplete="off" id="tripdateinput" type="date" name="tripdate" required>

What this does is create an eventListener to your input field. Similar to a single onChange eventHandler, this will get the event when your input field changes it's values.

Your code should look like this:

<input autocomplete="off" id="tripdateinput" type="date" name="tripdate" required>

<script>
  document.getElementById('tripdateinput').addEventListener("change", function(event) {
    console.log(event.target.value);
  });
</script>
Prosy Arceno
  • 2,616
  • 1
  • 8
  • 32