I have an HTML code like this one:
<form action="#" method="get">
<input type="date">
</form>
I want to have an event or something that will check when the date is changed, and get the date in JS Date object?
I have an HTML code like this one:
<form action="#" method="get">
<input type="date">
</form>
I want to have an event or something that will check when the date is changed, and get the date in JS Date object?
Try change
event handler on the input
,
$(document).ready(function(){
$('input[type="date"]').change(function(){
alert(this.value); //Date in full format alert(new Date(this.value));
var inputDate = new Date(this.value);
});
});
this[0].value ?
for me the jQuery selector returns an array
Dec 27, 2016 at 12:58
this
is just one context : the input that has currently changed. So it will never return an array in this
.
Dec 28, 2016 at 4:37
$('input[type="date"]')
that gives an array.
Dec 30, 2016 at 1:34
Attach the change
event handler and convert the value into Date
:
$("input").on("change", function () {
var myDate = new Date($(this).val());
console.log(myDate, myDate.getTime());
});
You need to use a .change() jquery handler it will look something like this:
$(document).ready(function(){
$('#dateInput').change(function(){
console.log('Updated Date');
});
});
<form>
<input id="dateInput" type="date" />
</form>