jQuery with date inputs

Sometimes, jQuery just seems like magic.

Recently, I was looking through the new input types in HTML5. Of course, most of these are not supported by IE9, but the author of the article I was reading suggested coders use some of the types anyway, since rendering engines default to type text. Therefore:

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

would show up as a text field in browsers that don’t support the date type and they would render as the date type in browsers which implement that feature of HTML5.

This seemed like a great idea, and then I realized that if I was already using jQuery UI, I could add “dateify” all the date-type text fields with the following code.

$('input[type="date"]').datepicker();

Maybe it’s a trival change from the way I used to do things (using a class as the jQuery selector), but I feel like the code is a little tighter, uses less keystrokes, and is just as readable.

One caveat though, the date input type is used by some mobile browsers to invoke their own date widget. In these situations, I don’t know who would win, or if it would just cause a horrible train-wreck.

Leave a Reply

Your email address will not be published. Required fields are marked *