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

Question

Question

How to: Use a checkbox to change required value of a specific address and field in a collection

asked on January 4, 2021 Show version history

I have a form that has a checkbox, two addresses a field for a phone number, and a few other fields not important to my issue. I have assigned the class of one the address as contractorAddress, the checkboxes class is contractorAffiliate the phone fields class is simply phone.

When the check box is checked I want to make it so the contractorAddress and phone are not required (in the current collection) and then if unchecked set it back to required. I do not want to change the requirement setting for the other address or any fields in added collections.

My code (see below) currently changes all instances of the contractorAddress and phone as well as the unnamed secondary address in the collection.

So what I'm asking is how to target the correct address and phone fields.

  $('.contractorAffiliate input').change(function () {
    if ($(this).is(':checked')) {

      
      $('.phone span.cf-required').hide();
      $('.phone input').removeClass('required').removeAttr('required'); 
      
      $('.contractorAddress span.cf-required').hide();                                      
      $('.contractorAddress input').removeClass('required').removeAttr('required'); 
      
    } else {

      
      $('.phone span.cf-required').show();
      $('.phone input').attr('required', 'True');   
      
      /* addresses are collections so each required field needs set as required accordingly */
      $('.contractorAddress span.cf-required').show();    

      $('.Address1').prop('required',true);                                          
      $('.City').prop('required',true);
      $('.State').prop('required',true);
      $('.Postal').prop('required',true);
    }
  })

 

0 0

Answer

SELECTED ANSWER
replied on January 4, 2021

I have a post on this topic that may help

Table and Collection JavaScript Event Handlers - Laserfiche Answers

The short version, is that you have to use more specific selectors. If your code is retrieving everything with a matching class, it will affect every field with that class even if it is in a different row/set.

The easiest option is to only look for matches within the common parent. The update/reply I added the post linked above should have examples for how to accomplish that.

1 0
replied on January 5, 2021

Thanks, your link was invaluable. With it I updated my code to the following and it is up and running correctly.

  $('.contractorInformation').on('change','.contractorAffiliate input',function(){
    
    // find sibling field in the collection row
    var thePhoneSpan = $(this).parents('ul.rpx').find('.phone span.cf-required');
    var thePhoneInput = $(this).parents('ul.rpx').find('.phone input'); 
    var theContractorAddress = $(this).parents('ul.rpx').find('.contractorAddress');
    var theContractorAddress1 = $(this).parents('ul.rpx').find('.contractorAddress .Address1');
    var theContractorCity = $(this).parents('ul.rpx').find('.contractorAddress .City');
    var theContractorState = $(this).parents('ul.rpx').find('.contractorAddress .State');
    var theContractorPostal = $(this).parents('ul.rpx').find('.contractorAddress .Postal');    
    var theContractorAddressSpan = $(this).parents('ul.rpx').find('.contractorAddress span.cf-required');
    var theContractorAddressInput = $(this).parents('ul.rpx').find('.contractorAddress input');
    
    
    
    if ($(this).is(':checked')) {
      thePhoneSpan.hide();
      thePhoneInput.removeClass('required').removeAttr('required');
      
      theContractorAddressSpan.hide();
      theContractorAddressInput.removeClass('required').removeAttr('required');      
      
      
    } else {
      thePhoneSpan.show();
      thePhoneInput.attr('required', 'True');
      
      theContractorAddressSpan.show();
      theContractorAddress1.prop('required',true); 
      theContractorCity.prop('required',true); 
      theContractorState.prop('required',true); 
      theContractorPostal.prop('required',true); 
      
    };

  }); 
 

 

0 0

Replies

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

Sign in to reply to this post.