Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
Get date directly from a calendar without the need for a button Javascript/HTML

I'm trying to get a date directly from an HTML calendar input, with a simple console log (for testing purposes), without the need for a button.

I've tried a few variations of the code below:

<div class = "Comparisons_Wrapper">
    <input type="date" class="DateSelector" id = "DateSelector" onclick="getDate()">
</div>
<script>
    function getDate(){
        var date = document.getElementById("DateSelector").value;
        console.log(date)
    }
</script>

I was hoping the onclick part of the div, would call the function on clicking the date from the calendar, but instead calls the function on immediately clicking the input box to open up the calendar, so you initially get an empty console.log the first time, and then when you click on the box again, you get the date you initially entered. I guess this assumption was naïve of me.

Any assistance would be appreciated,

Thanks James

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

What you want to do in this case is to add an event listener to the input's change event.

var dateInput = document.getElementById("DateSelector");
        
dateInput.addEventListener('change', function(event) {
   console.log(event.target.value)
});
<div class = "Comparisons_Wrapper">
    <input type="date" class="DateSelector" id = "DateSelector">
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use events like onchange, as follow:

function handler(e){
  alert(e.target.value);
}

<input type="date" class="DateSelector" id="DateSelector" onchange="handler(event);">
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda