SP2010 - Hiding Fields on Form Based upon Other Fields

Jun 23, 2014 at 1:03 PM
Edited Jun 23, 2014 at 1:05 PM
Hey guys, I have a form that has multiple fields on it. There are 2 fields that I am concerned with. One is a multiple choice field(Disposition) with different options, and the 2nd is a date field(Result Determination Date). What I am trying to accomplish is this. I want the 2nd field (Result Determination) to not be visible unless the first field (Disposition) is populated. Below is the current code I am using, but the issue is this. It works fine, until you bring up a form that already has Disposition populated. It won't display the Result Determination Date unless you change the selector. It's like it's not seeing the field is populated. I believe it is due to the "change(function" portion, since it's not really changing. Is there a better solution in SPServices for something like this?
$(document).ready(function(){
  
  $('nobr:contains("Result Determination Date")').closest('tr').hide();
  
 $("select[title='Disposition']").change(function() {
  if ($("select[title='Disposition']").val() != "") { 
  $('nobr:contains("Result Determination Date")').closest('tr').show();
  } else if($("select[title='Disposition']").val() == ""){ 
  $('nobr:contains("Result Determination Date")').closest('tr').hide();
  } 
 });
});</script>  
Coordinator
Jun 23, 2014 at 1:16 PM
Here you're not using SPServices, but just jQuery.

You can fire the change function yourself after you've defined it. This will do the check that you want, see that the value is populated, and display the date.
$(document).ready(function(){
  
  $('nobr:contains("Result Determination Date")').closest('tr').hide();
  
 $("select[title='Disposition']").change(function() {
  if ($("select[title='Disposition']").val() != "") { 
    $('nobr:contains("Result Determination Date")').closest('tr').show();
  } else if($("select[title='Disposition']").val() == ""){ 
    $('nobr:contains("Result Determination Date")').closest('tr').hide();
  } 
 });
 $("select[title='Disposition']").change();
});
M.
Jun 23, 2014 at 1:19 PM
I guess that'll work too! Haha... I understand I wasn't using SPServices... just didn't know if there was a better option in SPServices for something like this.....

Works great. Thanks again Marc!