Help with client script to select/deselect all check boxes

jas101
Tera Expert

Good evening all.

One of our field values 'Domain' has the option of 'Cross-domain' - if this value is chosen various checkboxes with different values appear. One of these check-box options is 'Global' - which when checked means all options (domains) are checked automatically. This is done via an onChange client script which is working fine:

function onChange(control, oldValue, newValue) {

    var global = g_form.getValue('u_member_firm_impacted_global');

      if (global == 'true') {

  g_form.setValue('u_member_firm_impacted_nrfa', 'true');

        g_form.setValue('u_member_firm_impacted_nrfcan', 'true');

        g_form.setValue('u_member_firm_impacted_nrfllp', 'true');

        g_form.setValue('u_member_firm_impacted_nrfsa', 'true');

      g_form.setValue('u_member_firm_impacted_nrfus', 'true');

      }}

There is currently another onChange another client script which exists to deselect the checkboxes if 'Global' is unchecked:

function onChange(control, oldValue, newValue) {

    var global = g_form.getValue('u_member_firm_impacted_global');

      if (global == 'false') {

  g_form.setValue('u_member_firm_impacted_nrfa', 'false');

        g_form.setValue('u_member_firm_impacted_nrfcan', 'false');

        g_form.setValue('u_member_firm_impacted_nrfllp', 'false');

        g_form.setValue('u_member_firm_impacted_nrfsa', 'false');

        g_form.setValue('u_member_firm_impacted_nrfus', 'false');

}}

Again this works in the unselect behaviour but the issue is that if 'Global' is not the checkbox checked and any others are e.g. NRFA and NRFCAN then on update of the record, these values are not saved as of course the above script is saying to blank out all values if 'Global' is false.

Screen Shot 2015-11-05 at 22.04.38.png

Any help on how best to solve this problem sincerely appreciated. I can obviously make the above client script inactive but it would be nice to have the option if Global is unticked to deselect all the checkboxes.

On a different note, if anyone knows how I can reduce the height of the 'Please select all regions....' message that would be a lovely bonus!

Thank-you!

Daniel

1 ACCEPTED SOLUTION

Kalaiarasan Pus
Giga Sage

I would create a function as below for the simplicity of code maintenance. If you want to change fields in future, you will just need to edit the array and the code will still continue to work



function selectUnselectFields(fieldName,display)


{


  var fieldsArray=fieldName.toString().split(',');


  var stringResult='';


  for(var i=0;i<fieldsArray.length;i++)


  {


  stringResult += "g_form.setValue(\""+ fieldsArray[i] + "\","+display+");";


  }


  eval(stringResult);


}




function onLoad() {


  var fieldName=["u_check_1","u_check_2","u_check_3"];


  selectUnselectFields(fieldName,true);


}



Once the function is defined, you can create a onchange client script and call the above function with the field names and a parameter to denote if the checkboxes need to be selected or unselected.


View solution in original post

20 REPLIES 20

Hey Kalai, I hope a quick one for you but as I think I said these check-box options appear if you choose 'Cross-domain' as the value in the field 'Domain'



But there is the risk someone will choose, Cross-domain then change the Domain option, how I can clear all checkboxes if domain is not 'Cross-domain (just like they now clear if 'Global' is unticked)? Thanks!


Just call the same function and pass the field names array in the first parameter and second parameter as false.

Kalai, excuse my lack of skills - but by this do you mean create a new onChange client script based on the Domain field? Thanks!


I think so. Cross domain is not a check box right what you are setting using global field?

Hi Kalai, 'Domain' field is a drop-down field which has 'Cross-domain' has one of its choice values, if 'Cross-domain' is selected the 'Global (Selects all)' and all other relevant check-boxes appear (so only when Cross-domain is selected as the Domain).


Thanks!