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

Question

Question

clear field when form opened

asked on November 2, 2022

I have a process that uses subsequent forms as it moves through the process.  When moving to the final form, I would like to clear data from several fields (including date fields), forcing users to update the information, while keeping data in the rest of the fields. 

Is there a way to use javascript to clear existing data only in select fields when the form is opened?

I'm a novice at this, so any help is appreciated.

0 0

Replies

replied on November 2, 2022 Show version history

Something like this should work.

Give all of your fields that you want cleared the class name of clearThisField.  Then add this Javascript: 

$(document).ready(function() {
  
  //When the form is loaded, loop through all fields with the 
  //clearThisField class and clear them.
  $('.clearThisField').each(function() {
    $(this).find('input').val('');                         //Clear single line fields.
    $(this).find('textarea').val('');                      //Clear multi-line fields.
    $(this).find('select').val('');                        //Clear drop-down fields.
    $(this).find('input:checked').prop('checked', false);  //Clear checkbox and radio button fields.
  });
  
  //This line is optional, but if included, the validation will be run on
  //the form after clearing them from the code above, and any required
  //fields will be marked as failing the validation.
  $('#form1').parsley().validate();
  
});

 

This is my form without the Javascript:

 

And this is my form with the Javascript:

1 0
replied on November 22, 2022

This is great, thanks for this. I tried this on one of my processes and it works nicely. Only thing is if I have a lookup on one of those cleared fields, they do not populate. Any workaround for that part?

1 0
replied on November 22, 2022

It depends entirely on what is triggering the lookups.  If us setting one of the fields to a blank value should be the trigger, you can try triggering the change event on the field.

For example, line 6 above says: 

$(this).find('input').val('');


You can add a change trigger a couple ways, including like this: 

$(this).find('input').val('').trigger('change');

 

0 0
replied on November 23, 2022 Show version history

@████████

Don't you also need to reset/change the vo attribute for fields previously set by a lookup?

1 0
replied on November 23, 2022

You might be right.  

However, it is still unclear if the fields we are emptying in JS are the results of the lookups or the triggers.

Depending on how that shakes out, there are likely a couple steps to get the lookups to re-run.

1 0
replied on November 23, 2022 Show version history

If I had to guess, I would say it is clearing out the lookup data. In the form, on the 'CSS and JavaScript' tab, the field I have this JS connected to, the default data is removed and replaced with my lookup data (on the right side of the screen). But when I display, or run an instance, it does not.

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

Sign in to reply to this post.