Hoser
Hoser

Reputation: 5044

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?

Upvotes: 11

Views: 70675

Answers (2)

mixel
mixel

Reputation: 25836

$('input[name=Date]').val(today);

Upvotes: 4

Sushanth --
Sushanth --

Reputation: 55740

For input just use .val()

To read the value

  z=$(date).val();

To set the value

$(date).val(today);

Upvotes: 22

Related Questions