0

I am trying to see what value my date input field has however I am always left with null in my console.

I am using the following JS to console.log my date input value:

document.getElementById('date_of_birth').addEventListener('change', function() {
    console.log(this.getAttribute('value'));
});

Here is my input field also:

<input id="date_of_birth" 
class="input input--bravo pad-1/2 span-1x field-date_of_birth is-invalid" 
data-palm="span-1x" 
placeholder="dd/mm/yyyy" 
pattern="\d{2}\/\d{2}\/\d{4}*"
name="payload[date_of_birth]" 
type="date" _flx1_12_1="1" _flx1_13_1="1">

When I select a date my JS triggers however prints null into the console. Why doesn't it show the date I have selected? I only want to see the date so I can see how the value is formatted so I can setup some validation for it.

Thanks, Nick

Sankar
  • 6,908
  • 2
  • 30
  • 53
Nick Maddren
  • 661
  • 4
  • 8
  • 20
  • 1
    Possible duplicate of [Difference between Element.value and Element.getAttribute("value")](http://stackoverflow.com/questions/11973678/difference-between-element-value-and-element-getattributevalue) – Álvaro González Oct 03 '16 at 09:55

4 Answers4

0

Try with console.log(this.value); instead of console.log(this.getAttribute('value'));

this.getAttribute will catch the HTML atrribute

Steeve Pitis
  • 4,283
  • 1
  • 21
  • 24
0
document.getElementById('date_of_birth').addEventListener('change', function() {
    console.log($('#date_of_birth').getDate());
});

You can use above Code to get Date of that HTML element.

dush88c
  • 1,918
  • 1
  • 27
  • 34
0

You can Try:

document.getElementById('date_of_birth').addEventListener('change', function() {
    console.log(this.value);
});

OR

document.getElementById('date_of_birth').addEventListener('change', function() {
    console.log(document.getElementById('date_of_birth').value);
});
Kumar_Vikas
  • 837
  • 7
  • 16
0

Try this...

getAttribute may used for defined or custom properties. You should use .value

document.getElementById('date_of_birth').addEventListener('change', function() {
  console.log(this.value);
});
<input id="date_of_birth" 
       class="input input--bravo pad-1/2 span-1x field-date_of_birth is-invalid" 
       data-palm="span-1x" 
       placeholder="dd/mm/yyyy" 
       pattern="\d{2}\/\d{2}\/\d{4}*" 
       name="payload[date_of_birth]" 
       type="date" _flx1_12_1="1" _flx1_13_1="1">

you can use getAttribute in this below case.

document.getElementById('date_of_birth').addEventListener('change', function() {
  console.log(this.getAttribute('value'));
});
<input id="date_of_birth" 
       class="input input--bravo pad-1/2 span-1x field-date_of_birth is-invalid" 
       data-palm="span-1x" 
       placeholder="dd/mm/yyyy" 
       pattern="\d{2}\/\d{2}\/\d{4}*" 
       name="payload[date_of_birth]" 
       value="01/01/2016"
       type="date" _flx1_12_1="1" _flx1_13_1="1">
Sankar
  • 6,908
  • 2
  • 30
  • 53