Auto-select single option in linked lookup field?

By: Piet Boekema | Asked: 05/02/2025
ForumsCategory: General questionsAuto-select single option in linked lookup field?
Piet Boekema asked 3 weeks ago

I have created two lookup fields in formidableforms, in lookup 1 you choose the location and in the second field (lookup 2) there are working types, you can choose multiple work types per location, but sometimes there is only 1 choice. Is it possible that when there is only one choice that is directly visible instead of that I have to select it manually?

I have tried with some Javascript, is that the right way?

Attachments
Victor Font Staff replied 2 weeks ago

I received your reply through email. I'm reposting it here for reference:

"Hello Victor, first of all thank you very much for youre reaction, I made this script en put this in a html field in the used form:

(function initRepeaterLogic() { const locatieVelden = document.querySelectorAll('[id^="field_1897-"]'); locatieVelden.forEach(function(locatieVeld) { locatieVeld.addEventListener('change', function () { const veldIndex = locatieVeld.id.split('-')[1]; const werksoortVeld = document.getElementById('field_1898-' + veldIndex); setTimeout(function () { if (werksoortVeld && werksoortVeld.options.length === 2) { werksoortVeld.selectedIndex = 1; werksoortVeld.dispatchEvent(new Event('change', { bubbles: true })); } }, 600); }); });})();

It does nog work right now. I am in doubt of the field ID's. Can you look at this script, am i on the right track?

Best regards,

Piet Boekema"

Victor Font Staff replied 2 weeks ago

"I am in doubt of the field ID's."

So am I!

Formidable forms field ids by "field_" + [key]. Unless, you changed the field hey to be numeric, they are not formatted correctly. Use your browser's inspection tool to see what the field #'s are truly.

1 Answers
Victor Font Staff answered 3 weeks ago

Use jQuery to change the selected indicator in the field when the page loads. The function you need to create needs to count the number of values and mark it as selected if there's only one.

Piet Boekema replied 2 weeks ago

Hello Victor, thanks. I am looking at he jQuery now.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right