Skip to main content
studiom50222188
Participating Frequently
April 29, 2024
Answered

Show fields based on text entry

  • April 29, 2024
  • 1 reply
  • 681 views

Hello, I want to create a form where a user can type a product code on a text field and show 3 other text fields which are (description of the current product code, competitor A's product, comptetitor B's product)

 

I've come across this tutorial and it's close to what I want to do, but instead of the drop-down I want to be able to type in the value in a text field and the relevant info would show up.

 

Follow up question, I have over a hundred product codes to add so I'm not sure if this is the best way to do it but I figure I'd start here, thanks

Correct answer Nesa Nurani

You can use this as 'Validate' script of 'Code' field, assuming 3 fields are named "Competitor A,B and C", just fill in 'cList' with your data nothing else needs to be changed:

var cList = [
{code: "001", cA:"Description for competitor A", cB:"Description for competitor B", cC:"Description for competitor C"},
{code: "002", cA:"Description for competitor A", cB:"Description for competitor B", cC:"Description for competitor C"},
{code: "003", cA:"Description for competitor A", cB:"Description for competitor B", cC:"Description for competitor C"}];

function updateFields(selectedCode) {
 var found = false;

 for (var i = 0; i < cList.length; i++) {
  if (selectedCode === cList[i].code) {
   this.getField("Competitor A").value = cList[i].cA;
   this.getField("Competitor B").value = cList[i].cB;
   this.getField("Competitor C").value = cList[i].cC;
   found = true;
   break;}}

  if (!found) {
   this.getField("Competitor A").value = "";
   this.getField("Competitor B").value = "";
   this.getField("Competitor C").value = "";}}

updateFields(event.value);

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
April 29, 2024

You can use this as 'Validate' script of 'Code' field, assuming 3 fields are named "Competitor A,B and C", just fill in 'cList' with your data nothing else needs to be changed:

var cList = [
{code: "001", cA:"Description for competitor A", cB:"Description for competitor B", cC:"Description for competitor C"},
{code: "002", cA:"Description for competitor A", cB:"Description for competitor B", cC:"Description for competitor C"},
{code: "003", cA:"Description for competitor A", cB:"Description for competitor B", cC:"Description for competitor C"}];

function updateFields(selectedCode) {
 var found = false;

 for (var i = 0; i < cList.length; i++) {
  if (selectedCode === cList[i].code) {
   this.getField("Competitor A").value = cList[i].cA;
   this.getField("Competitor B").value = cList[i].cB;
   this.getField("Competitor C").value = cList[i].cC;
   found = true;
   break;}}

  if (!found) {
   this.getField("Competitor A").value = "";
   this.getField("Competitor B").value = "";
   this.getField("Competitor C").value = "";}}

updateFields(event.value);
studiom50222188
Participating Frequently
April 30, 2024

Hi Nesa, 

This is great it worked for me (just changed Code to selectedCode)

Thank you!