← Script library

Client Scripts

Dynamic Field Visibility

Show or hide fields based on another field's value (e.g., show "Other reason" field only when user selects "Other" from dropdown).

JavaScript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  // Configuration: Define visibility rules
  // Key = field value that triggers showing fields
  // Value = array of fields to show
  var visibilityRules = {
    'other': ['u_other_reason', 'u_other_details'],
    'hardware': ['u_hardware_type', 'cmdb_ci'],
    'software': ['u_application_name', 'u_software_version']
  };

  // Hide all conditional fields first
  Object.keys(visibilityRules).forEach(function(key) {
    visibilityRules[key].forEach(function(field) {
      g_form.setDisplay(field, false);

      // Optional: Clear the field when hiding
      if (!isLoading) {
        g_form.clearValue(field);
      }
    });
  });

  // Show fields based on current value
  if (newValue && visibilityRules[newValue]) {
    visibilityRules[newValue].forEach(function(field) {
      g_form.setDisplay(field, true);
    });
  }
}

How to use it

1. Create an onChange Client Script on your table 2. Set the field to your controlling field (e.g., category) 3. Customize the `visibilityRules` object with your field values and fields to show 4. Create a duplicate script as onLoad type with the same logic for initial form load

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.