How to remove the placeholder text of input[type="date"]?
Asked Answered
C

4

7

I have an input[type="date"] with min/max range. What I am trying to achieve is to hide the placeholder text displayed in any language as of dd/mm/yyyy. What have tried so far is adding the following CSS.

input[type="date"]:in-range::-webkit-datetime-edit-year-field, 
input[type="date"]:in-range::-webkit-datetime-edit-month-field, 
input[type="date"]:in-range::-webkit-datetime-edit-day-field {
    color: transparent;
}

But that doesn't produce the intended output, as I have a range validator on the element.

Cydnus answered 30/8, 2020 at 6:56 Comment(1)
What trying to achieve is to have the dd/mm/yyyy hidden at all times, even on focus in/out, while at same time have the "value" entered visible at all timesCydnus
G
3

You could swap out placeholders using a ::before pseudo-element:

input[type="date"]::before {
  content: attr(placeholder);
  position: absolute;
  color: #999999;
}

input[type="date"] {
  color: #ffffff;
}

input[type="date"]:focus,
input[type="date"]:valid {
  color: #666666;
}

input[type="date"]:focus::before,
input[type="date"]:valid::before {
  content: "";
}
<input type="date" placeholder="Date" required>
Gain answered 30/8, 2020 at 7:40 Comment(1)
Playing around with this a bit on developer.mozilla.org/en-US/docs/Web/HTML/Element/input/… it seems only input[type="date"] { color: #ffffff; } input[type="date"]:focus, input[type="date"]:valid { color: #666666; } is neededPadova
C
2

The following works on Chrome. I am not sure about the other browsers. Safari doesn't seem to have an input of type date

const $input = $('input');
$input.on('input', function(){
    $input.addClass('focused')
});
input::-webkit-datetime-edit { 
    color: transparent;
    user-select: none;
}

.focused::-webkit-datetime-edit{ 
  color: #000; 
  user-select: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='Date' />

Check out this Stack Overflow post for more information

Commuter answered 30/8, 2020 at 7:0 Comment(8)
tried adding the above to my css, but still the placeholder dd/mm/yyyy appears? isn't the dd/mm/yyyy considered a placeholder or is there another pseudo-element to allocate that display text??Cydnus
this hides the placeholder correctly, but disallows selecting and inputting date, the date input doesn't show the selected values anymore and doesn't allow inputting data by keyboardCydnus
You can now see it when you focus the input.Commuter
nice, but what am trying to achieve, as a requirement, is to have the value field always visible, while not have the placeholder visible; non visible value on focus out doesn't satisfy the requirement of the applicationCydnus
The latest edit hides the dd/mm/yyyy, but after focusing, it shows the value always. Hope that helpsCommuter
It uses jQuery but.Commuter
JQuery is no issue, the issue is that will need the dd/mm/yyyy hidden at all times, both when focus is in/outCydnus
not when focus is in, it still prints the dd/mm/yyyyCydnus
S
0

On pure HTML & Javascript the following code can be of use

<input
  type="date"
  name="dateInput"
  id="dateInput"
  value=""
  style="padding-left: 4.2rem;"
/>

<script>
  document.addEventListener("DOMContentLoaded", function () {
    var dateInput = document.getElementById("dateInput");

    dateInput.addEventListener("input", function () {
      var inputValue = dateInput.value;

      if (inputValue) {
        dateInput.style.paddingLeft = "0rem";
      } else {
        dateInput.style.paddingLeft = "4.2rem";
      }
    });
  });
</script>

On Angular

 <input
      type="date"
      name="dateInput "
      value="{{ dateInput }}"
      [(ngModel)]=" dateInput "
      [ngStyle]="{'padding-left': dateInput ? '0rem' : '4.2rem'}"
    />

this code just pushes the default mm/dd/yyyy out side of the width of the input .. the 4.2rem is just what i used for my case and you'll need to change the value to suit your needs.

Standley answered 25/9, 2023 at 20:0 Comment(0)
F
-1

Placeholder?

input[type="date"]::before{
content: attr(placeholder);
position: absolute;
color: rgba(0,0,0,0);
}

input[type="date"]{color: rgba(0,0,0,1);}
Filomena answered 30/8, 2020 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.