Saltar al contenido

StudiosJohan

				
					<script>const showThisFieldIf = {
  part1: {
    servicesselect: [2],
  },
  part2: {
    servicesselect: [3],
  },
  part3: {
    servicesselect: [4],
  },
};

function conditionalFormFieldFunc() {
  function testLogic() {
    for (const [conditionalInputID, condition] of Object.entries(showThisFieldIf)) {
      const conditionalInput = setInputsElemArray(conditionalInputID);
      let match = true;
      for (const [conditionID, conditionValues] of Object.entries(condition)) {
        const inputs = setInputsElemArray(conditionID);
        const selectedInputs = [];
        inputs.forEach((input, i) => {
          if (input.checked) {
            selectedInputs.push(i);
          }
        });
        if (inputs[0].tagName === 'SELECT') {
          selectedInputs.push(inputs[0].selectedIndex);
        }
        const adjustedConditionValues = conditionValues.map(e => e - 1);
        if (!(adjustedConditionValues.every(condition => selectedInputs.indexOf(condition) > -1))) {
          match = false;
        }
      }
      if (match) {
        conditionalInput.forEach(e => e.closest('.elementor-field-group').style.display = "block");
      } else {
        conditionalInput.forEach(e => e.closest('.elementor-field-group').style.display = "none");
      }
    }
  }

  function setInputsElemArray(ID) {
    const selectors = `[name="form_fields[${ID}]"]`;
    let inputs = Array.from(document.querySelectorAll(selectors));
    if (!inputs.length) {
      inputs = Array.from(document.querySelectorAll(`[name="form_fields[${ID}][]"]`));
    }
    return inputs;
  }

  testLogic();

  /* Add event listeners */
  for (const [conditionalInputID, condition] of Object.entries(showThisFieldIf)) {
    for (const [conditionID, conditionValues] of Object.entries(condition)) {
      const inputs = setInputsElemArray(conditionID);
      inputs.forEach(input => {
        input.addEventListener('input', function () {
          testLogic();
        });
      });
    }
  }
}

document.addEventListener('DOMContentLoaded', conditionalFormFieldFunc);
jQuery(document).on('elementor/popup/show', (event, id, instance) => {
  conditionalFormFieldFunc();
});


</script>