You are viewing limited content. For full access, please sign in.

Question

Question

How to show a field only if other text fields differ?

asked on May 22, 2020

Is it possible to have a field populate only if a certain set of fields is different? Here's what I'm working with: 

My goal is to have the Special Remarks field shown only when there is a difference in last name in the tables above.

I'm currently trying to get the "Show Special Remarks?" field to populate a value based on a formula that I'm using: IF(Last1=Last2=Last3,"Same","Different"), though I'm not even sure if the IF function can handle that logic. I'm planning to hide that field and then use it to determine whether or not to display the "Special Remarks" field, as that is required if the last names of the individuals on this form are different.

One potential complication is that not all three of those tables (self, spouse, and child) will always be in use. Depending on what is selected previously in the form, one, two, or all three of these will be required. To further add to the complexity, additional rows can be added to the child table.

Is this even possible? If so, any help would be greatly appreciated!

0 0

Answer

SELECTED ANSWER
replied on May 22, 2020 Show version history

In that case, give this a try:

const lastnameID_self = ?;
const lastnameID_spouse = ?;
const lastnameID_children = ?;
const lastnameResultID = ?;

$(document).on("change", '[data-col="q'+lastnameID_self+'"], [data-col="q'+lastnameID_spouse+'"], [data-col="q'+lastnameID_children+'"]', function() {
  var fields = $('[data-col="q'+lastnameID_self+'"] input, [data-col="q'+lastnameID_spouse+'"] input, [data-col="q'+lastnameID_children+'"] input');
  
  var different = false;
  var valFirst = "";
  $(fields).each(function() {
    var val = $(this).val();
    if (valFirst == "")
      valFirst = val;
    else if (val != "" && valFirst != val)
      different = true;
  });
  
  var diffText = different ? "Different" : "Same";
  $("#q"+lastnameResultID+" input").val(diffText).change();
});

Replace the first three lines' question marks with the ID numbers of the last name column for each of the three tables, and the fourth line (lastnameResultID) for the hidden field, as before.

0 0

Replies

replied on May 22, 2020 Show version history

Hi Dylan,

I'd recommend using JavaScript. The in-field calculations are likely not advanced enough for comparing the values of every row in a table.

Create a hidden field that is not read-only, and set your field rule to show the Special Remarks when the hidden field is "Different". (You may have already completed both of these steps if you were testing your example field calculation.)

Then, add the following JavaScript and change the question mark in the first line ("lastnameID") to the column's ID number, and the question mark in the second line ("lastnameResultID") to the ID of the hidden field that triggers the field rule from my previous paragraph.

var lastnameID = ?;
var lastnameResultID = ?;

$(document).on("change", '[data-col="q'+lastnameID+'"]', function() {
  var different = false;
  var valFirst = "";
  $('[data-col="q'+lastnameID+'"] input').each(function() {
    var val = $(this).val();
    if (valFirst == "")
      valFirst = val;
    else if (val != "" && valFirst != val)
      different = true;
  });
  var diffText = different ? "Different" : "Same";
  $("#q"+lastnameResultID+" input").val(diffText).change();
});

FYI you can find the ID of the column by right clicking on one of the column's fields and choosing Inspect, and then in the HTML it's the number between "Field" and "(" in the "id" attribute. So for example in this:

The field's "id" attribute is "Field4(1)", so the column's ID is 4.

0 0
replied on May 22, 2020

Jim,

Thank you very much for this response! Unless I'm misunderstanding, this appears like it would work for one table, but not several, correct? The screenshot I provided is actually 3 separate tables with their titles hidden via CSS. A "self" table, restricted to one row, a "spouse" table, restricted to one row, and a "child" table, with the ability to add rows as needed.

0 0
SELECTED ANSWER
replied on May 22, 2020 Show version history

In that case, give this a try:

const lastnameID_self = ?;
const lastnameID_spouse = ?;
const lastnameID_children = ?;
const lastnameResultID = ?;

$(document).on("change", '[data-col="q'+lastnameID_self+'"], [data-col="q'+lastnameID_spouse+'"], [data-col="q'+lastnameID_children+'"]', function() {
  var fields = $('[data-col="q'+lastnameID_self+'"] input, [data-col="q'+lastnameID_spouse+'"] input, [data-col="q'+lastnameID_children+'"] input');
  
  var different = false;
  var valFirst = "";
  $(fields).each(function() {
    var val = $(this).val();
    if (valFirst == "")
      valFirst = val;
    else if (val != "" && valFirst != val)
      different = true;
  });
  
  var diffText = different ? "Different" : "Same";
  $("#q"+lastnameResultID+" input").val(diffText).change();
});

Replace the first three lines' question marks with the ID numbers of the last name column for each of the three tables, and the fourth line (lastnameResultID) for the hidden field, as before.

0 0
replied on May 22, 2020

You are a wizard Jim. That worked! Thanks so much for the assistance!

1 0
replied on July 2, 2020

Hi Jim! To follow up on this, what modifications would I need to make to be able to repeat this across multiple other instances of the exact example I initially provided?

You are not allowed to follow up in this post.

Sign in to reply to this post.