'Show placeholder text for input type date
Placeholder does not work for input type date
and datetime-local
directly.
<input type="date" placeholder="Date" />
<input type="datetime-local" placeholder="Date" />
Instead the field shows mm/dd/yyy on desktop and nothing on mobile.
How can I show the Date placeholder text?
Solution 1:[1]
use onfocus="(this.type='date')"
, example:
<input required="" type="text" class="form-control" placeholder="Date" onfocus="(this.type='date')"/>
Solution 2:[2]
use onfocus and onblur... Here it's an example:
<input type="text" placeholder="Birth Date" onfocus="(this.type='date')" onblur="if(this.value==''){this.type='text'}">
Solution 3:[3]
Here, I have tried data
attribute in input
element. and applied required placeholder using CSS
<input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" />
input[type="date"]::before {
content: attr(data-placeholder);
width: 100%;
}
/* hide our custom/fake placeholder text when in focus to show the default
* 'mm/dd/yyyy' value and when valid to show the users' date of birth value.
*/
input[type="date"]:focus::before,
input[type="date"]:valid::before { display: none }
<input type="date" name="dob" data-placeholder="Date of birth" required aria-required="true" />
Hope this helps
Solution 4:[4]
<input type="text" placeholder="*To Date" onfocus="(this.type='date')" onblur="(this.type='text')" >
This code works for me. Simply you can use this
Solution 5:[5]
For angular 2,You can use this directive
import {Directive, ElementRef, HostListener} from '@angular/core';
@Directive({
selector: '[appDateClick]'
})
export class DateClickDirective {
@HostListener('focus') onMouseFocus() {
this.el.nativeElement.type = 'date';
setTimeout(()=>{this.el.nativeElement.click()},2);
}
@HostListener('blur') onMouseBlur() {
if(this.el.nativeElement.value == ""){
this.el.nativeElement.type = 'text';
}
}
constructor(private el:ElementRef) { }
}
and use it like below.
<input appDateClick name="targetDate" placeholder="buton name" type="text">
Solution 6:[6]
Modern browsers use a Shadow DOM to facilitate input of dates and datetimes. As a result, placeholder text is not shown unless the browser chooses to fallback to a text
input for whatever reason. You can accommodate both scenarios with the following logic:
::-webkit-calendar-picker-indicator {
@apply hidden; /* hide native picker icon */
}
input[type^='date']:not(:placeholder-shown) {
@apply before:content-[attr(placeholder)];
@apply sm:after:content-[attr(placeholder)] sm:before:hidden;
}
input[type^='date']::after,
input[type^='date']::before {
@apply text-gray-500;
}
I've used Tailwind CSS syntax because it's easy to grok. Let's break it down piece by piece:
::-webkit-calendar-picker-indicator {
@apply hidden; /* hide native picker icon */
}
Hide native picker icon (generally a calendar) using its Shadow DOM pseudo-element selector.
input[type^='date']:not(:placeholder-shown) {
@apply before:content-[attr(placeholder)];
@apply sm:after:content-[attr(placeholder)] sm:before:hidden;
}
Select all date
and datetime-local
inputs where placeholder is not shown and:
- Show the input
placeholder
text using the::before
pseudo-element by default - On small screens and above switch to using the
::after
pseudo-element instead
input[type^='date']::after,
input[type^='date']::before {
@apply text-gray-500;
}
Style the ::before
and ::after
pseudo-elements.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Elyor |
Solution 2 | Leandro Da Silva |
Solution 3 | Rohan Khude |
Solution 4 | Ajinz |
Solution 5 | |
Solution 6 | vhs |