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

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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