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

Question

Question

Possible to force a change - JS

asked on June 27, 2024

We're working on converting our HR Personnel Action form into LF and we have a lot of rules we'd like to apply as far as what should be required or read-only based on what action is selected.  We have a ton of lookup rules associated as well, so we'd like to open the form with 'new hire' as the default action, but I can't make the rules apply correctly without selecting another action and then going back.  Is it possible to force a change or another way of doing it, when the form opens, where it thinks someone selected it?  I think this may be built in with Forms11, but we're not there yet.

Below is what I'm starting with and would like to build off that for each action.  Any insight or ideas would be greatly appreciated.  Thanks!

$(document).ready(function () {
    $('.chkReason input').change(function () { 
      
        var isChecked = $(this).is(':checked');
        var selectedOption = $(this).val(); 
      
      if (selectedOption === 'New_Hire' && isChecked) {
            $('.req label').append('<span class="cf-required">*</span>');
            $('.req input').attr('required', 'True');
        } else { //if box is not checked
            $('.req span.cf-required').remove();
            $('.req input').removeClass('required').removeAttr('required');
        }
    })
})

 

0 0

Replies

replied on June 27, 2024

The below line should force the change event to trigger

$('.chkReason input').change();

 

0 0
replied on June 27, 2024

Thanks, @████████.  Where would you include that line in the code?

0 0
replied on June 27, 2024

Either just after the "$(document).ready" line or just before the last "})" line.

0 0
replied on June 28, 2024

I tried both but neither worked.  Still have to click on another selection and then back on "new hire" for the actions to take place.

I'll try to find another work around.  I appreciate your assistance!

0 0
replied on June 28, 2024

The problem might be that your JS is firing off before the form loads. It's been a little while since I've done something like this, but I think I got it working using a setTimeout to wait a few seconds and then trigger the change.

0 0
replied on June 28, 2024

I ended up setting the parameters I needed before the "change function" line.  Seems to be okay for now.

Is there a way to reset the form after each change?  For example, if x is checked, reset form, and then do 1, 2, and 3??

 

Thanks

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

Sign in to reply to this post.