Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear value of input type date in Microsoft Edge

Microsoft Edge displays a HTML5 date picker to enter values for inputs defined by

<input type="date">

Is there a way to show a clear button or an 'x' decoration to delete the value afterwards, like it is done by Google Chrome?

like image 573
mitchbrand Avatar asked Sep 17 '15 11:09

mitchbrand


People also ask

What is the default date format in edge for Windows 10?

Windows 10 regional system defaults are set correctly and system dates are showing in the dd/mm/yyyy format but everything in Edge defaults to mm/dd/yyyy Was this reply helpful? 'Everything' is a lot. Can you give an example of a page in Edge where the date format is wrong? Or the URL of a web page ditto?

Does edge support preset values for date?

CanIUse says that edge does support 'date'. So is this just a bug or does Edge not support preset values? Show activity on this post. This is not just Edge-related. Any HTML5 compliant browser that supports the various input types related to date and time expects the input's value to be in ISO 8601 format. For a date type, that would be YYYY-MM-DD.

Does edge support'date'values?

I inspected the element and found that even though it is displaying 'mm/dd/yyyy' it still had the correct value. CanIUse says that edge does support 'date'.

Why does edge use the default placeholder for date and time?

This is not just Edge-related. Any HTML5 compliant browser that supports the various input types related to date and time expects the input's value to be in ISO 8601 format. For a date type, that would be YYYY-MM-DD. If the value of the field is not in this format, it is treated as non-existent, so you get the default placeholder.


1 Answers

The best option here is probably to hide the custom clear mark provided by Chrome, and provide your own for all browsers to use instead:

input::-webkit-clear-button {
    display: none;
}

You would then provide your own clear button:

<input type="date" required />
<button>Clear</button>

And finally, a small piece of logic to clear the input whenever the button is clicked:

(function () {

    "use strict";

    var elmnt = document.querySelector( "input" );
    var clear = document.querySelector( "button" );

    clear.addEventListener( "click", function ( event ) {
        elmnt.value = "";
    });

}());

I hope this helps.

Fiddle: http://jsfiddle.net/zcay9qc5/

like image 165
Sampson Avatar answered Oct 22 '22 12:10

Sampson