0

how can I change the value of a field based on the value of another field programatically?

This other field has already a value but I need to replace / update it.

To be more precise:

If the Text Field named type has value Houses AND another Text Field named service has value 2, this should be updated in NOT AVAILABLE.

Or ...

If the Text Field named type has value Parks AND another Text Field named service has value 2, this should be updated in AVAILABLE.

Or ...

If the Text Field named type has value Store AND another Text Field named service has value 2, this should be updated in COMING SOON.

I read the update_field doc but I could not solve this.

1
  • Hello and welcome to WPSE @Richdn. Please make sure you read these guidelines on questions and update your question as necessary. I'm not sure this is within the scope of WPSE either and may be better suited to SO
    – Bysander
    Commented May 13, 2022 at 10:51

1 Answer 1

1

Did you trying something like this in a JS script?

Assuming you wants to edit the service text field.

jQuery(document).ready(() => {
  const textfield_type = document.querySelector('input[name="type"]');
  const textfield_service = document.querySelector('input[name="service"]');

  const updateField = () => {
    if (textfield_type.getAttribute('value') === 'Houses' && textfield_service.getAttribute('value') === '2') {
      textfield_service.setAttribute('value', 'NOT AVAILABLE');
    }

    // ... other rules
  }

  // Launch the event only if the two fields exists
  if (textfield_type !== null && textfield_service !== null) {
    textfield_service.addEventListener('focusout', () => updateField());
  }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.