55

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.

Shkarik
  • 1,139
  • 2
  • 9
  • 17

15 Answers15

99

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" />
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
Roman
  • 13,100
  • 2
  • 47
  • 63
35

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>
Abdo-Host
  • 2,470
  • 34
  • 33
15

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">

FIDDLE

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
Abdullah Gheith
  • 521
  • 6
  • 19
14

You can do something like this:

<input onfocus="(this.type='date')" class="js-form-control" placeholder="Enter Date">
Oday Hazeem
  • 405
  • 4
  • 7
5

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.

As noted here

Xenology
  • 2,357
  • 2
  • 21
  • 39
5

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)" />
felixmosh
  • 32,615
  • 9
  • 69
  • 88
4

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).

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
2

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>
Yeza
  • 21
  • 2
0

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.

CaribouCode
  • 13,998
  • 28
  • 102
  • 174
0

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.

Community
  • 1
  • 1
Guillaume Gendre
  • 2,504
  • 28
  • 17
0

As i mentioned here

initially set the field type to text.

on focus change it to type date

Community
  • 1
  • 1
sidarcy
  • 2,958
  • 2
  • 36
  • 37
0

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");
            }
        });
    });
Saeid
  • 422
  • 4
  • 9
-1

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.

Raphael Jeger
  • 5,024
  • 13
  • 48
  • 79
-1

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">
jsdev
  • 672
  • 6
  • 14
-3

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 />
Himalaya
  • 11
  • 1