Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Don't understand why this isn't working. I have a simple 'input type="date"' field as such....

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

And I'm trying to set the value to todays date whenever the 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've done the normal debugging and I know this function is being called and I know that the variable 'today' does in fact hold todays date in the form 'yyyy-mm-dd'. I have tried doing all different types of date formats (dd/mm/yyyy, dd-mm-yyyy, etc.)

Any idea why this isn't working?

like image 902
Hoser Avatar asked Jun 03 '13 04:06

Hoser


People also ask

Can we change input type date format?

Users can type a date value into the text field of an input[type=date] with the date format shown in the box as gray text. This format is obtained from the operating system's setting. Web authors have no way to change the date format because there currently is no standards to specify the format.

How can set current date in textbox using jquery?

datepicker. formatDate("dd-mm-yy", new Date())); Add the above code at the end of the script.


2 Answers

For input just use .val()

To read the value

  z=$(date).val();

To set the value

$(date).val(today);
like image 74
Sushanth -- Avatar answered Sep 28 '22 23:09

Sushanth --


$('input[name=Date]').val(today);
like image 35
mixel Avatar answered Sep 29 '22 00:09

mixel