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

230
Visualizações
How to add timestamp conditionally for a checkbox value in jQuery?

I am trying to add a timestamp in the checkbox input field as a value. But its always shows the same value.

How to add a different value ( Blank ) while unchecking?

Here is he HTML Code,

<div class="flex items-center mb-4 newsletter-check">                    
<input type="checkbox" id="newsletter" name="join-my-newsletter" class="form-check" value="Yes">
<label class="form-check-label" for="newsletter">Join my newsletter</label>
</div>

I used the javascript

$(function() { // when page loads
  var timeNow = new Date().getTime();
$(".newsletter-check input[type='checkbox']").on("click",function() {
if (this.checked) $("#newsletter").attr('value',  + timeNow );
});
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can try this code:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $(".newsletter-check input[type='checkbox']").on("click",function() {
    var timeNow = new Date().getTime();
    if ($(this).prop('checked')==true){
     $("#newsletter").attr('value', timeNow );
     alert(timeNow);
    }else{
     $("#newsletter").attr('value', '');
    } 
  });
});
</script>
</head>
<body>

<div class="flex items-center mb-4 newsletter-check">                    
<input type="checkbox" id="newsletter" name="join-my-newsletter" class="form-check" value="Yes">
<label class="form-check-label" for="newsletter">Join my newsletter</label>
</div>

</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

Timestamp gets set on page load so it does not update.
You can move the timestamp declaration inside the click event so it gets updated whenever you trigger the checkbox.

$(function() { // when page loads
$(".newsletter-check input[type='checkbox']").on("click",function() {
  var timeNow = new Date().getTime();
  console.log(timeNow);

  if (this.checked) $("#newsletter").attr('value',  + timeNow );
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="flex items-center mb-4 newsletter-check">                    
<input type="checkbox" id="newsletter" name="join-my-newsletter" class="form-check" value="Yes">
<label class="form-check-label" for="newsletter">Join my newsletter</label>
</div>

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