An accessible polyfill for <input type='time'/>
elements.
- ✔️ Modeled after the Chrome and Firefox desktop implementations.
- ✔️ Fully keyboard and screen reader accessible.
- ✔️ Submits the same values to servers as real time inputs.
- ✔️ Only downloads the full polyfill code in the browsers that need it.
- ✔️ Zero dependencies.
Demo available here: https://dan503.github.io/time-input-polyfill/
Note: If the recommended version in this documentation is out of sync with the npm version, this is because npm only allows readme edits to be committed through full releases. To prevent needless cache invalidation, I'll only update the recommended version number when there are actual changes to the polyfill code. The current recommended version is 1.0.7
. As long as you are using a version that is equal to or higher than that, you are using the latest version of the polyfill.
Add the following script element to your page:
<script src="https://cdn.jsdelivr.net/npm/time-input-polyfill"></script>
Alternatively you can download it via npm and use it through commonJS or an ES6 import statement.
npm i time-input-polyfill
Then require it in your main JavaScript file like so:
// ES5
require('time-input-polyfill/auto');
// ES6
import 'time-input-polyfill/auto';
That's all you need to do.
You didn't load the actual polyfill onto the page, you loaded a much smaller automatic initialiser function instead.
- The initialiser checks if the browser supports
input[type="time"]
elements. - If it does, it skips the rest of the functionality.
- If it does not, it will:
- load
https://cdn.jsdelivr.net/npm/time-input-polyfill@1.0.7/dist/time-input-polyfill.min.js
(the actual polyfill). - Collect all existing
input[type="time"]
elements on the page. - Loop through each
input[type="time"]
element and apply the polyfill to it.
- load
The following downloads the full polyfill in all browsers, take a look at the auto.js file if you want to see how it loads the polyfill dynamically.
First check for input[type="time"]
support.
var supportsTime = require('time-input-polyfill/supportsTime');
if (!supportsTime) {
//Apply polyfill here
}
Then gather a list of all input[type="time"]
elements on the page, and loop through them to apply the polyfill.
var supportsTime = require('time-input-polyfill/supportsTime');
var TimePolyfill = require('time-input-polyfill');
if (!supportsTime) {
// Converting to an array for IE support
var $$inputs = [].slice.call( document.querySelectorAll('input[type="time"]') );
$$inputs.forEach(function($input){
new TimePolyfill($input);
})
}
TimePolyfill
in this case will be a function that is only accessible from the file that it was required in.
First check for input[type="time"]
support.
<script src="https://cdn.jsdelivr.net/npm/time-input-polyfill@1.0.7/core/helpers/supportsTime.js"></script>
if (!supportsTime) {
//Apply polyfill here
}
Then gather a list of all input[type="time"]
elements on the page, and loop through them to apply the polyfill.
<script src="https://cdn.jsdelivr.net/npm/time-input-polyfill@1.0.7/core/helpers/supportsTime.js"></script>
<script src="https://cdn.jsdelivr.net/npm/time-input-polyfill@1.0.7/dist/time-input-polyfill.min.js"></script>
if (!supportsTime) {
// Converting to an array for IE support
var $$inputs = [].slice.call( document.querySelectorAll('input[type="time"]') );
$$inputs.forEach(function($input){
new TimePolyfill($input);
})
}
This will add a global TimePolyfill
function to the page.
Note that I refer to an input[type="time"]
element that has had the polyfill initialized on it as an $input
in this section.
In browsers that support the time input natively, they will provide the value
of the input in 24 hour time (eg. 20:45
). The polyfill will provide the value in 12 hour time (08:45 PM
). If the polyfill detects that a form is being submitted, the polyfill will quickly switch to 24 hour time in an attempt to align with standard time input functionality.
If this isn't working for you, you can prevent the auto-swap functionality by setting $input.polyfill.autoSwap = false
. You can access the current input value in 24 hour time format by reading the data-value
attribute.
You can also switch the $input
manually to 24 hour time using $input.polyfill.swap()
. The polyfill does not function properly at the moment while running in 24 hour time. 24 hour time is only meant to be used as a means of submitting correct values to forms. It is not intended to be used as a permanent mode.
I couldn't find any reliable way to track when a user uses $input.value = '13:30'
. So instead of tracking the use of $input.value
, I have attached a .polyfill.update()
method to the $input
element.
Any time you update the value of the time input element through JavaScript, check that $input.polyfill
exists, and if it does, call $input.polyfill.update()
.
<input id="example" type="time">
var $input = document.getElementByID('example');
$input.value = '13:30';
// call the update() method whenever the value is updated through JS
if ($input.polyfill) $input.polyfill.update();
The update()
method will return the input element that it was called on so it can be chained if you want.
The polyfill will fail if the $input
is missing a label.
The following is a list of ways you can add a label to the $input
. The list is in order from the best method to the worst method:
- Nesting the
$input
inside a<label>
(Doesn't require IDs to work)<label> <span>Label text</span> <input type="time"> </label>
- Using the
for
attribute<label for="uniqueID">Label text</label> <input type="time" id="uniqueID">
- Using the
aria-labelledby
attribute<p id="uniqueID">Label text</p> <input type="time" aria-labelledby="uniqueID">
- Using the
title
attribute<input type="time" title="Label text">
- Using the
aria-label
attribute<input type="time" aria-label="Label text">
You can view the Change Log on the GitHub Releases page.
Please make pull requests against Develop branch rather than Master.
For testing you will need Gulp cli installed (npm i gulp-cli -g
) then run gulp --open
from a command line interface.