Reputation: 1159
It's impossible to use placeholder on date fields but I really need it.
I want two date inputs with texts "From" and "To" on each one as placeholders.
Upvotes: 56
Views: 150209
Reputation: 623
just use it :
var elementDate = $('input[type="date"]');
$.each(elementDate, (key, value) => {
if (!$(value).val()) {
$(value).css("color", "#777");
}
$(value).on("focus", function (event) {
$(this).css("color", "#000");
});
$(value).on("blur", function (event) {
if (!$(this).val()) {
$(this).css("color", "#777");
}
});
});
Upvotes: 0
Reputation: 7618
The input[type="date"]
DOMElement only takes the following value: YYYY-MM-DD
, any other format or text with be skipped.
var element = document.querySelectorAll('[placeholder]');
for (var i in element) {
if (element[i].nodeType == 1 && element[i].nodeName == "INPUT") {
element[i].value = element[i].getAttribute('placeholder');
element[i].style.color = "#777";
element[i].onfocus = function(event) {
if (this.value == this.getAttribute('placeholder')) {
this.value = "";
this.style.color = "#000"
};
};
element[i].onblur = function(event) {
if (this.value == "") {
this.value = this.getAttribute('placeholder');
this.style.color = "#777";
}
};
}
}
<input type="text" placeholder="bingo" />
<input type="date" placeholder="2013-01-25" />
In this exact case, with 2 input
elements, Pure JavaScript is ~40% ± 10% faster. With 32 input
elements, the difference remains the same (~43% ± 10% faster for Pure JS).
Upvotes: 4
Reputation: 520
You can use CSS's before pseudo.
.dateclass {
width: 100%;
}
.dateclass.placeholderclass::before {
width: 100%;
content: attr(placeholder);
}
.dateclass.placeholderclass:hover::before {
width: 0%;
content: "";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<input
type="date"
placeholder="Please specify a date"
onClick="$(this).removeClass('placeholderclass')"
class="dateclass placeholderclass">
Upvotes: 15
Reputation: 21
Got the most easiest hack for this problem-use this syntax in your HTML-input-tag
<input type="text" id="my_element_id" placeholder="select a date" name="my_element_name" onfocus="(this.type='date')" />
</div>
Upvotes: 2
Reputation: 35493
I'm using this css method in order to simulate placeholder on the input date.
The only thing that need js is to setAttribute of the value, if using React, it works out of the box.
input[type="date"] {
position: relative;
}
input[type="date"]:before {
content: attr(placeholder);
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #fff;
color: rgba(0, 0, 0, 0.65);
pointer-events: none;
line-height: 1.5;
padding: 0 0.5rem;
}
input[type="date"]:focus:before,
input[type="date"]:not([value=""]):before
{
display: none;
}
<input type="date" placeholder="Choose date" value="" onChange="this.setAttribute('value', this.value)" />
Upvotes: 5
Reputation: 4103
use this input attribute events
onfocus="(this.type='date')" onblur="(this.type='text')"
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="container mt-3">
<label for="date">Date : </label>
<input placeholder="Your Date" class="form-control" type="text" onfocus="(this.type='date')" onblur="(this.type='text')" id="date">
</div>
Upvotes: 36
Reputation: 672
Using a jQuery (sure you can achieve this with Vanilla. This make sure the date format still the same.
$(function() {
$('.sdate').on('focus', function() {
$(this).attr('type', 'date');
});
$('.sdate').on('blur', function() {
if(!$(this).val()) { // important
$(this).attr('type', 'text');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type="text" class="sdate" placeholder="From">
Upvotes: -1
Reputation: 5232
Ok, so this is what I have done:
$(document).on('change','#birthday',function(){
if($('#birthday').val()!==''){
$('#birthday').addClass('hasValue');
}else{
$('#birthday').removeClass('hasValue');
}
})
This is to remove the placeholder when a value is given.
input[type="date"]:before {
content: attr(placeholder) !important;
color: #5C5C5C;
margin-right: 0.5em;
}
input[type="date"]:focus:before,
input[type="date"].hasValue:before {
content: "" !important;
margin-right: 0;
}
On focus or if .hasValue, remove the placeholder and its margin.
Upvotes: -1
Reputation: 11
CSS
input[type="date"] {position: relative;}
input[type="date"]:before {
position: absolute;left: 0px;top: 0px;
content: "Enter DOB";
color: #999;
width: 100%;line-height: 32px;
}
input[type="date"]:valid:before {display: none;}
<input type="date" required />
Upvotes: -3
Reputation: 425
You can do something like this:
<input onfocus="(this.type='date')" class="js-form-control" placeholder="Enter Date">
Upvotes: 15
Reputation: 13058
I'm using the following CSS only trick:
input[type="date"]:before {
content: attr(placeholder) !important;
color: #aaa;
margin-right: 0.5em;
}
input[type="date"]:focus:before,
input[type="date"]:valid:before {
content: "";
}
<input type="date" placeholder="Choose a Date" />
Upvotes: 101
Reputation: 3008
As i mentioned here
initially set the field type to text.
on focus change it to type date
Upvotes: 0
Reputation: 2524
As mentionned here, I've made it work with some ":before" pseudo-class and a small bit of javascript. Here is the idea :
#myInput:before{ content:"Date of birth"; width:100%; color:#AAA; }
#myInput:focus:before,
#myInput.not_empty:before{ content:none }
Then in javascript, add the "not_empty" class depending on the value in the onChange and onKeyUp events.
You can even add all of this dynamically on every date fields. Check my answer on the other thread for the code.
It's not perfect but it's working well in Chrome and iOS7 webviews. Maybe it could help you.
Upvotes: 0
Reputation: 2452
The HTML5 date input field actually does not support the attribute for placeholder. It will always be ignored by the browser, at least as per the current spec.
Upvotes: 5
Reputation: 14398
Try this:
Add a placeholder attribute to your field with the value you want, then add this jQuery:
$('[placeholder]').each(function(){
$(this).val($(this).attr('placeholder'));
}).focus(function(){
if ($(this).val() == $(this).attr('placeholder')) { $(this).val(''); }
}).blur(function(){
if ($(this).val() == '') { $(this).val($(this).attr('placeholder')); }
});
I've not tested it for fields that can't take placeholders but you shouldn't need to change anything in the code at all.
On another note, this code is also a great solution for browsers that don't support the placeholder attribute.
Upvotes: 0