Remove default text/placeholder present in html5 input element of type=date

前端 未结 10 2144
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 19:32

I am using html input element with type as date,


When I use above element it creates a default date format i.e.

相关标签:
10条回答
  • 2020-11-27 19:53

    This works in chrome and doesn't make the value dissapear when set (Version 74)

    input[value=""]::-webkit-datetime-edit{ color: transparent; }
    input:focus::-webkit-datetime-edit{ color: #000; }
    
    0 讨论(0)
  • 2020-11-27 19:54

    Actually you can take advantage of the default placeholder generated by Chrome by using the following code. This code also works with dates fetched from the database:

    <div class="Input">
        <script>
            function getDate() {
                var GET_DATE = document.getElementById("ThisElement").value="<?php echo $GetDateFromMySQL = date('d/m/Y', strtotime($row_FetchedResults["MySQLColumnName"]));?>";
                return  GET_DATE;
            }
        </script>
    
        <input class="form-control"
               style=" text-align: left; border: none;" 
               onfocus="(this.type='date')"  
               onblur="
                   if(this.value===''){
                       this.type='text';
                       this.value='';
                       this.value= getDate();
                   }
                   else{
                       this.value;
                   }"                       
               id="ThisElement"  
               type = "text" 
               value = "<?php echo $GetDateFromMySQL = date('d/m/Y', strtotime($row_ActiveStudents["ChaseUpDate"]));?>"                
         />
    
    0 讨论(0)
  • 2020-11-27 19:57
    ::-webkit-datetime-edit-year-field:not([aria-valuenow]),
    ::-webkit-datetime-edit-month-field:not([aria-valuenow]),
    ::-webkit-datetime-edit-day-field:not([aria-valuenow]) {
        color: transparent;
    }
    
    0 讨论(0)
  • 2020-11-27 20:02

    Possible option

    HTML:

    <input type='date' required>
    

    CSS:

    input[type=date]:required:invalid::-webkit-datetime-edit {
        color: transparent;
    }
    input[type=date]:focus::-webkit-datetime-edit {
        color: black !important;
    }
    
    0 讨论(0)
  • 2020-11-27 20:02

    I had similar problem with datetime-local, this worked for me, maybe someone can use it. Text input is much easier than datetime to style

    type='text' required='' onfocus="this.type='datetime-local'" onblur="if(this.value==='')this.type='text'"
    
    0 讨论(0)
  • 2020-11-27 20:04

    This should be transparent only when value is not set.

    input[value="0000-00-00"]::-webkit-datetime-edit {
         color: transparent; 
    }
    
    0 讨论(0)
提交回复
热议问题