Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change max or min value of input type date from JS

I have two input type dates like:

<input type="date" name="first_date">

and other like:

<input type="date" name="second_date" min="first_date">

now what I want to do is that when first_date is selected than the minimum range of the second_date is auto filled by javascript.

Any idea how to do this.

like image 280
mega6382 Avatar asked Dec 07 '25 10:12

mega6382


2 Answers

Basic JavaScript using an onchange event to set the attribute.

document.getElementById("firstDateId").onchange = function () {
    var input = document.getElementById("secondDateId");
    input.setAttribute("min", this.value);
}

Code could be better using addEventListener.

like image 74
epascarello Avatar answered Dec 10 '25 00:12

epascarello


I agree with epascarello’s answer, only you can replace setAttribut("min/max") with simply .min or .max.

document.getElementById("firstDateId").onchange = function ()
{
  var input = document.getElementById("secondDateId");
  input.min = this.value;
}
like image 40
Y2H Avatar answered Dec 10 '25 01:12

Y2H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!