How do you programmatically clear HTML5 date fields?

后端 未结 10 1261
我在风中等你
我在风中等你 2020-12-11 16:18

I\'m looking for a way to programmatically clear HTML5 date fields with Javascript (specifically jQuery). So far I have tried two methods which I thought obvious:

         


        
10条回答
  •  無奈伤痛
    2020-12-11 16:24

    I needed to do it recently and i've made this little hack... Seems to do the job.

    It was just with JavaScript, but the jQuery version is pretty the same...

    function reset_date_native() {
      var date_input = document.getElementById('date-id');
    
      //erase the input value
      date_input.value = '';
    
      //prevent error on older browsers (aka IE8)
      if (date_input.type === 'date') {
        //update the input content (visually)
        date_input.type = 'text';
        date_input.type = 'date';
      }
    }
    
    function reset_date_jquery() {
      $('#date-id').val('')
        .attr('type', 'text')
        .attr('type', 'date');
    }
    
    
    
    

提交回复
热议问题