Ramos Alcarez Jr
Ramos Alcarez Jr

Reputation: 19

Is it possible to use a JavaScript Value for a min value in a html element?

For example if

var D1 = D2.val(); 

Is there a way I can use this variable as a minimum in the HTML element such as:

<input type = "Date" min="">

Upvotes: 0

Views: 65

Answers (3)

Brewal
Brewal

Reputation: 8189

Give your input an id (i.e. myinput) and try :

document.getElementById("myinput").setAttribute("min", D1);

If you want to get the min value from another date input, you have to use events in order to update the min value of the other input :

var dateInputStart = document.getElementById("dateInputStart"),
    dateInput = document.getElementById("dateInput");

dateInputStart.onchange = function(){
    dateInput.setAttribute('min', dateInputStart.value);
};

jsFiddle Demo

Upvotes: 3

Danield
Danield

Reputation: 125473

You could simply use dot notation to set the attribute:

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

var D1 = '1900-01-01';
document.getElementById("dateInput").min = D1;

Upvotes: 0

user5548116
user5548116

Reputation:

Yeap it possible:

<input type = "number" id='inp' >

var minNumber = 0;
document.getElementById('inp').setAttribute('min', minNumber);

The same for Dates and etc.

Fiddle

Upvotes: 1

Related Questions