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

Question

Question

Javascript Populated Field Still Shows Missing Requirement

asked on January 6, 2017 Show version history

I have a dropdown field that is hidden by default.  I have a button on my form that when clicked, it makes the field appear and automatically selects the default value in the dropdown.  This is working.  However, the field is being flagged as a missing required field even though it has been populated.  If I tab through the field, it'll update and remove the required field error.  Does anyone know how to programmatically trigger the form to recognize that the field has been updated and drop the required field error?

I've tried:
$('.myDropdown option').trigger('change');
and 
$('.myDropdown option').blur();
without success.

0 0

Answer

SELECTED ANSWER
replied on January 6, 2017

I inadvertently solved this problem.  I hadn't realized (yet) that while the fields were hidden, they were still showing as required fields, and would consequently prevent the form from being submitted.  Once I realized this, I added code to flag the field as not required when it is hidden, and then change it back to required when it is shown.  Once I did that, this problem went away.

Here's the code when hiding the field:

$('.myDropdown select').hide();
$('.myDropdown select').removeAttr('required');

 

And here's the code when showing the field:

$('.myDropdown select').show();
$('.myDropdown select').attr('required',true);

 

0 0

Replies

replied on January 6, 2017

Hi Matthew,

How are you using JavaScript to fill the dropdown? I can submit the form fine using this JavaScript code:

$(document).ready(function() {
  $('#pressMe').on('click',function() {
    $('.dropdown select').val('A');
  });
});

where ".dropdown" is the class I assigned to the drop-down field, and "#pressMe" is a button I added in a custom HTML field...that part's not relevant. You should be able to just set the value of the select element to the value of the dropdown option.

Note that the value must be set, not the label. Also with this method if the value does not exist in the specified list, it won't do anything. I have not yet tested this in conjunction with calculations, field rules, or lookup rules.

0 0
replied on January 6, 2017

Setting the "selected" prop of one of the option elements in the dropdown should also work, based on my testing. But I always use the approach outlined by James - it's cleaner and more readable.

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

Sign in to reply to this post.