Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda