You are viewing limited content. For full access, please sign in.

Question

Question

Is there a way to update date field in Forms using JS?

asked on May 28, 2015

Hi Everyone,

I have a form where we're retrieving values from a SQL lookup. Some of these fields are date fields. When the date field is null, we get a value of 1/1/1900 on the date field in Forms. I need to clear out these values when that happens, and ideally I would be using JS to accomplish this. However, I cannot seem to get it to work. This is the code I'm using (date fields are using CSS class of 'Date'):

$(document).ready(function () {

$('.Date').on('change lookup', ClearDate);

function ClearDate() {
    if ($('.Date input').val()=='1/1/1900'){
      $('.Date input').val()='';
    }
  } 

});

I'm really stuck on this and I don't know where to go from here. Any suggestions are welcome!

0 0

Answer

SELECTED ANSWER
replied on May 28, 2015

You can use

$(document).ready(function () {

  $('.Date').on('change lookup', ClearDate);

  function ClearDate() {
    $('.Date input').each(function () {
      if ($(this).val()=='1/1/1900'){
        $(this).val('');
      }
    });
  } 

});
0 0

Replies

replied on May 28, 2015

Your Javascript code is slightly off. Instead of

$('.Date input').val()='';

you need to use

$('.Date input').val('');

 

1 0
replied on May 28, 2015

Thank you Alex,

That was indeed an error on the code. However, upon correcting it, the code still won't work for all fields with class Date. If I set up individual functions and classes (say, Date1, Date2, Date3 with functions ClearDate1, ClearDate2, ClearDate3) then it works. Is there a way to generalize this behavior?

0 0
SELECTED ANSWER
replied on May 28, 2015

You can use

$(document).ready(function () {

  $('.Date').on('change lookup', ClearDate);

  function ClearDate() {
    $('.Date input').each(function () {
      if ($(this).val()=='1/1/1900'){
        $(this).val('');
      }
    });
  } 

});
0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.