HTML5 introduced a new date
input type which allows a user to enter a date using a date picker.
<input id="date" type="date" value="2018-05-26">
This is what it looks like in Chrome:
However, not all browsers support this input type. In unsupported browsers, such as Internet Explorer, you will simply see a text input field.
In this post, I will show how you can detect if a browser supports the date
input type and how you can fall back to using jQuery's date picker if it doesn't.
date
input:
The following code creates an input element and sets its type to date
. If the browser does not support date
input, this operation will not work and the input type will degrade to text
.
var input = document.createElement("input"); input.setAttribute("type", "date"); if (input.type !== "date") { console.log("browser does not support date input"); }Alternatively, use the Modernizr library, which makes it easy to detect the features that a browser supports:
if (!Modernizr.inputtypes.date) { console.log("browser does not support date input"); }Falling back to jQuery's date picker:
The JSFiddle below shows how you would use jQuery's date picker if the browser does not support date
input.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.