I'm trying to set the value of an input element using jQuery.
HTML:
<input id="id_year" name="year" type="number">
JavaScript:
$(document).ready(function () {
var year = $("#id_year");
year.datepicker({
format: " yyyy", // Notice the Extra space at the beginning
viewMode: "years",
minViewMode: "years"
});
year.on('changeDate', function(ev){
year.attr('value',ev.date.getFullYear());
});
});
If the input type is text, it works. But I have to use number as input type.
How can I set the input value with jQuery or regular JavaScript?
Important notes
I tried to use year.val(ev.date.getFullYear()); but It didn't work.
.attr()to set the value property of any form elements, you should be using.val(). (As an aside, why is there a space at the beginning of the format?)year.val(ev.date.getFullYear());, but It doesn't work!