Set the value of <input type = "date" ... in jquery

I do not understand why this does not work. I have a simple input field type = "date". As such ....

<input type="date" name="Date"/> 

And I'm trying to set a value for today's date whenever a page loads with this function ...

 function setDate(date){ z=$(date).attr('value'); var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; //January is 0! var yyyy = today.getFullYear(); if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = yyyy+'-'+mm+'-'+dd; $(date).attr('value',today); } 

I did the usual debugging, and I know that this function is being called, and I know that the variable "today" really stores today's date in the form of "yyyy-mm-dd". I tried to do all types of date formats (dd / mm / yyyy, dd-mm-yyyy, etc.)

Any idea why this is not working?

+6
source share
2 answers

For input just use .val ()

In read the meaning

  z=$(date).val(); 

To set a value

 $(date).val(today); 
+17
source
 $('input[name=Date]').val(today); 
+4
source

Source: https://habr.com/ru/post/946374/


All Articles