Select date and load the value automatically

3

What, what I try to imply is that I need the user to select a date and without clicking on the me button of the selected date, I have searched for events in JS but I am a bit lost, someone could advise me by please, thank you very much ..

I share the code:

I have a date where the user will choose:

<div class="col-md-12"> 
<label>FECHA: </label> 
<input type="date" name="fecha" id="fecha" required> 
</div>

I need to know the date you chose to know which day of the week you selected, but I need you to do it when you select the date because now you do it until you click on the button, but I need to do it without giving click on the button.

After the date, follow this code, what I do is compare your time of entry or exit, depending on the case ... I hope to understand ...

    
asked by Checo 27.03.2017 в 21:28
source

1 answer

2

As you want it to be right after selecting the date, I recommend the event onChange , which is activated when the value of the input has changed. (the onBlur that you recommend in the comments is when you leave the item)

The input date other than the value has valueAsDate that returns you a%% of%.

With it you can take the day (numeric) with Date with getDay()

Making a small array with the days of the week, and the index that returns the getUTCDay() we can know what day it is.

Example:

var dias = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"];


function diaSemana() {
    var x = document.getElementById("fecha");
    alert( "Día: " + dias[x.valueAsDate.getUTCDay()]);
}
<div class="col-md-12"> 
<label>FECHA: </label> 
<input type="date" name="fecha" id="fecha" onchange="diaSemana();" required> 
</div>
    
answered by 28.03.2017 / 08:56
source