Change a date input when changing select

Asked

Viewed 297 times

-1

I would like if user select a specific option of a select in the form as the second option another field of the date type of the same form change to today date automatically.

<input type="date" id="demoInput" /> 

<select id="demoSelect" />
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>

$('#demoSelect').change(function(){
    $('#demoInput').val($(this).val());
});

1 answer

1

Your code works correctly, the problem is that an input of the date type only receives values in the date format. Change the value of options to dates in the format yyyy-mm-dd and will work normal.

$('#demoSelect').change(function(){
    $('#demoInput').val($(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="demoInput" /> 

<select id="demoSelect" />
<option value="2019-01-01">01/01/2019</option>
<option value="2018-02-01">01/02/2018</option>
<option value="2017-12-05">05/12/2017</option>
</select>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.