2

Possible Duplicate:
What events does an <input type=“number” /> fire when it's value is changed?

I have a date input field

<input type="date" name="..">

In webkit and firefox, it will be added two up/down arrows on the side of the input. When clicking them, the date goes forward or backwards, but the onchange event doesn't trigger until the field loses focus. Is there a workaround for this?

Community
  • 1
  • 1
tbleckert
  • 3,763
  • 4
  • 32
  • 39
  • Duplicate question http://stackoverflow.com/search?q=oninput – mplungjan Apr 12 '11 at 10:00
  • jquery .change() might help you [http://api.jquery.com/change/](http://api.jquery.com/change/) – Alex Apr 12 '11 at 10:03
  • Thanks guys, no match came up when I wrote the title. Sorry :) – tbleckert Apr 12 '11 at 10:05
  • I do not see the arrows in Safari 5 or Fx4 http://diveintohtml5.org/examples/input-type-date.html – mplungjan Apr 12 '11 at 10:07
  • Yeah, I was wrong about Fx. But in Safari I see them :) – tbleckert Apr 12 '11 at 10:37
  • Please see this: http://stackoverflow.com/questions/5634412/isnt-this-html5-date-input-supposed-to-have-a-spinner-in-fx4-and-safari5/5634488#5634488 – mplungjan Apr 12 '11 at 12:14
  • This is a great page. I do not see any support for DATE anywhere than in my Opera http://diveintohtml5.org/forms.html#type-date and on date it is a date picker - what Safari on what platform do you have a spinner on Date? – mplungjan Apr 12 '11 at 12:19
  • strangely enough, i have found that the change event fires differently depending on how you create it (e.g. element.onchange= ... delays firing until focus is lost, whereas element.addEventListener fires on every change) – Michael Apr 25 '16 at 18:23

1 Answers1

3

You could store the value in a global and check for a difference onclick.

<input type="date" name="myDate" id="myDate" />

var myApp = {};
myApp.myDate = "";

document.getElementById('myDate').onclick = function (e) {
    if (this.value != myApp.myDate) {
        // run onchange code

        // then set last value to current value
        myApp.myDate = this.value;
    }
};

See example →

EDIT: Or, duplicate question, like comments suggest. (See oninput event)

mVChr
  • 49,587
  • 11
  • 107
  • 104