10

I'm trying to capture a paste event for an input[type="date"] element. In Chrome you cannot copy/paste into this type of element, so as a workaround I am trying to wrap it in a DIV element with an onpaste event. The issue I'm encountering is that if you click the date input and press CTRL+V nothing happens. However, if you click anywhere else in the body first and then click the date input and press CTRL+V, it works...

<div onpaste="alert('test')">
    <input type="date">
</div>

Demo:

https://jsfiddle.net/4qh31tn0/

EDIT: OK, so it turns out that the onpaste event doesn't have to be on the DIV, it can be moved to the INPUT element, but the problem persists. If I load the jsfiddle, click the input and press CTRL+V, nothing happens. If I click someplace outside of the INPUT element beforehand then click the input and press CTRL+V, it works...

mx0
  • 4,605
  • 10
  • 39
  • 47
Chris_F
  • 3,780
  • 4
  • 27
  • 44
  • This is beginning to look like a bug in Chrome that only affects date inputs. – Chris_F Aug 04 '16 at 18:51
  • Why you are not using keyup event and change date to text on dynamic and change again to date. – Silence Peace Aug 04 '16 at 18:52
  • Opened https://bugs.chromium.org/p/chromium/issues/detail?id=634426 – Chris_F Aug 04 '16 at 19:02
  • 1
    I just want to point out, though you probably already knew this, that `onpaste` isn't part of the HTML spec. If you get this working in one browser, it's unlikely to work in another. –  Aug 04 '16 at 19:17

1 Answers1

0

While this bug (I think it's a bug) isn't fixed you can instead detect if the date input has been changed using onkeydown, onkeyup, onclick, onblur and other events.

Community
  • 1
  • 1
Klaider
  • 3,140
  • 3
  • 20
  • 49
  • See my question again. It *does* work, but only if you have clicked someplace else on the page at least once before you try it. This is clearly a bug. – Chris_F Aug 04 '16 at 19:02
  • @Chris_F But that's the idea while it's a bug. If you don't want the bug happening, try to polyfill the `onchange` event instead, as said in the answer. – Klaider Aug 04 '16 at 19:19