I have been trying for last few days, no luck. Below is the issue.
Scenario ( CPT and backend based site ):
row 1 > sub_field-1 — sub_field-2
row 2 > sub_field-1 — sub_field-2
row 3 > sub_field-1 — sub_field-2
and so on…one can add as many rows he wants
What I wish to achieve
Automatically fetch value from sub_field-2 and load it in sub_field-1 with some modification
So for example a user enters in sub_field-2 ….’london’
Automatically or after saving post sub_field-1 should show…. ‘domain.com/?city=london’
Please someone help.
I have tried to implement all types of solution
But I cant just figure out.
1) build an action that fires on the keyup action of the first field
2) Get the value
3) get the closes repeater
repeater = e.$el.closest('.acf-repeater')
4) Then locate the sibling field
repeater.find('[data-key="field_12345678"] .acf-input input')
5) adjust the value an insert it into the other field
The other option is to do it when the post is saved. See acf/save_post https://www.advancedcustomfields.com/resources/acfsave_post/. Get the values from the repeater and use https://www.advancedcustomfields.com/resources/update_row/ to update the other field.
Every case of using custom JS for fields is pretty specific to what needs to be done and it’s really complicated to do anything generic. Every type of field needs different handling and that handling can be completely different when a field is in a repeater vs not in a repeater. Someone could make a full time job out of writing tutorials for it. I have some examples of getting and updating values with JS here https://github.com/Hube2/acf-dynamic-ajax-select-example might give you an idea of what’s involved.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.