How do I simulate placeholder functionality on input date field?

后端 未结 14 1381
生来不讨喜
生来不讨喜 2020-11-27 03:35

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.

相关标签:
14条回答
  • 2020-11-27 04:14

    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.

    0 讨论(0)
  • 2020-11-27 04:15

    The input[type="date"] DOMElement only takes the following value: YYYY-MM-DD, any other format or text with be skipped.

    Live Demo

    HTML

    <input type="text" placeholder="bingo" />
    <input type="date" placeholder="2013-01-25" />
    

    Pure JavaScript

    window.onload = function () {
        /* Grab all elements with a placeholder attribute */
        var element = document.querySelectorAll('[placeholder]');
    
        /* Loop through each found elements */
        for (var i in element) {
            /* If the element is a DOMElement and has the nodeName "INPUT" */
            if (element[i].nodeType == 1 && element[i].nodeName == "INPUT") {
    
                /* We change the value of the element to its placeholder attr value */
                element[i].value = element[i].getAttribute('placeholder');
                /* We change its color to a light gray */
                element[i].style.color = "#777";
    
                /* When the input is selected/clicked on */
                element[i].onfocus = function (event) {
                    /* If the value within it is the placeholder, we clear it */
                    if (this.value == this.getAttribute('placeholder')) {
                        this.value = "";
                        /* Setting default text color */
                        this.style.color = "#000";
                    };
                };
    
                /* We the input is left */
                element[i].onblur = function (event) {
                    /* If the field is empty, we set its value to the placeholder */
                    if (this.value == "") {
                        this.value = this.getAttribute('placeholder');
                        this.style.color = "#777";
                    }
                };
            }
        }
    }
    

    Performance review

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

    0 讨论(0)
  • 2020-11-27 04:16

    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

    0 讨论(0)
  • 2020-11-27 04:20

    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.

    0 讨论(0)
  • 2020-11-27 04:20

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

    0 讨论(0)
  • 2020-11-27 04:22

    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: "";
    }
    <input
      type="date"
      placeholder="Please specify a date"
      onClick="$(this).removeClass('placeholderclass')"
      class="dateclass placeholderclass">

    FIDDLE

    0 讨论(0)
提交回复
热议问题