Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
JavaScript Change click button for automatic

I have this script

<script>
        function calculaDiferenca(data_inicio, data_fim) {
          var date1 = new Date(data_inicio);
          var date2 = new Date(data_fim);
          var timeDiff = Math.abs(date2.getTime() - date1.getTime());
          var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
          alert(diffDays + ' dias');
      }

      (function() {
        
          var calcBtn = document.getElementById('calcular');
          
          calcBtn.addEventListener('click', function(){
            
              var data_inicio = document.getElementById('data_inicio').value;
              var data_fim = document.getElementById('data_fim').value;
              
              calculaDiferenca(data_inicio, data_fim);
          });

})();

    </script>  

Can I change instead of clicking the button to calculate being when date1 and date2 are filled do the calculation?

 var calcBtn = document.getElementById('calcular');
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The answer from @Khalil is good. I just want to add something to make the process sequential.

Make the date2 field disabled. As:

<input type="date" id="data_fim" disabled/>

Then make it dependent on date1.

let first = document.getElementById('data_inicio');
let last  = document.getElementById('data_fim');

first.addEventListener('change', (e) => {
  last.disabled = false
  calculaDiferenca(e.target.value, last.value)
})

last.addEventListener('change', (e) => {
  calculaDiferenca(first.value, e.target.value)
})
about 4 years ago · Santiago Trujillo Report

0

You can listen for change event on the input fields and invoke the calculation function.

let first = document.getElementById('data_inicio');
let last = document.getElementById('data_fim');

first.addEventListener('change', (e) => {
  calculaDiferenca(e.target.value, last.value)
})

last.addEventListener('change', (e) => {
  calculaDiferenca(first.value, e.target.value)
})
about 4 years ago · Santiago Trujillo Report

0

And here is a working snippet:

function calculaDiferenca(ev) {
  var date1 = new Date(ini.value);
  var date2 = new Date(fim.value);
  var timeDiff = Math.abs(date2.getTime() - date1.getTime());
  var diffDays = Math.ceil(timeDiff/(1000*3600*24));
  dif.textContent=diffDays+' dia'+(diffDays===1?'':'s');
  }

const [ini,fim,dif] = "data_inicio,data_fim,dif".split(",").map(t=>document.getElementById(t));
[ini,fim].forEach(e=>e.addEventListener("input",calculaDiferenca));



 
<input type="date" id="data_inicio" placeholder="inicio">
<input type="date" id="data_fim" placeholder="fim"><br>
<p>Diferenca: <span id="dif"></span></p>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!