11

Adding type date to input fields now produces a browser based date picker. (Where supported).

<input type="date"></input>

enter image description here

This is fantasic for touch devies however...

When browsing with the meat of the market: firefox and internet explorer, type date is not supported.


QUESTION

How to use input type="date" and fallback to a javascript date picker when support is not available?

Currently I cant seem to get the best of both worlds without producing both date pickers simultaneously.

1
  • Browsers that don’t support type=date should report the type as the default text when it’s queried via JS … Commented Jan 24, 2014 at 10:37

2 Answers 2

13

You should look into using modernizr which uses JS to work out what features the current browser has. In the below example you can serve another datepicker if this browser isn't compatible:

<script src="modernizr.js"></script>
<script>Modernizr.load({
  test: Modernizr.inputtypes.date,
  nope: ['http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js', 'jquery-ui.css'],
  complete: function () {
    $('input[type=date]').datepicker({
      dateFormat: 'yy-mm-dd'
    }); 
  }
});
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

This is great and would solve the issue. However I am strict about using excess code if it can be avoided. The overhead on this is a lot for what I want to achieve. Is it possible to do a compatibility check just for this one feature.
If you only want to use YepNope with Modernizr.load you can just use that. modernizr.com/download/#-load I tend to use Modernizr for feature detection quite a lot for other types of input, CSS transforms and html5shiv. It's almost a pre-requisit for my builds so I'd give a second thought to including more of it's feature detection too :)
Thanks @richieahb. I will take a deeper look at it.
You will also need the input detection in the modernizr bundle which you can find in this package modernizr.com/download/#-inputtypes-load and it's only 6kb.
Note that the 'nope' is now deprecated and won't work to conditionally load scripts etc. You will now have to use jQuery or something else to load the scripts (or always load them if you don't care about slower pages).
7

Have a look at this polyfill :

https://github.com/chemerisuk/better-dateinput-polyfill

2 Comments

Fallback script and type detector all in one. The date picker is actually smaller and better than the one I had to start with. Many thanks.
But with a lot of dependencies. JQuery is not enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.