5 Replies - 2478 Views - Last Post: 11 March 2014 - 10:50 AM

#1 stylus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-May 13

Problem with hiding 2 SharePoint fields with javascript/jQuery

Posted 07 March 2014 - 04:51 AM

I have some code which i've got which hides 2 SharePoint fields based on the values of the said fields.

I have 2 fields. 'Service Lead Approved?' and 'Funding Request Sanctioned?'. Both are boolean (yes/no) fields.
The code checks to see if the values of 2 fields are set to specific values and then hides either one or both fields from view dependant on their values.

Everything works as normal for 2 of the 3 outcomes.

IF 'Service Lead Approved?' and 'Funding Request Sanctioned?' both equal 'NO', then 'Funding Request Sanctioned?' is hidden.

IF 'Service Lead Approved?' equals 'YES' and 'Funding Request Sanctioned?' equals 'NO', then 'Funding Request Sanctioned?' is hidden.

However, when 'Service Lead Approved?' and 'Funding Request Sanctioned?' both equal 'YES', then 'Service Lead Approved?' IS hidden but 'Funding Request Sanctioned?' is not. In my code, both fields should be hidden and I cannot see why this isn't working.

Any help would be greatly appreciated.

<!-- SECTION 4.0: SHOW/HIDE 'SERVICE LEAD APPROVED'/'FUNDING REQUEST SANCTIONED?' OPTIONS -->
// since we will be accessing the collection of nobr elements multiple times,
// let's cache the collection for performance.
var nobr;
// define globally, set in document ready function (see below)
 
function showConditionalRows() {
      var okToShow = true;
      var hideALL = true;
      if ($("select[title='Service Lead Approved?']").val() == "Yes" && $("select[title='Funding Request Sanctioned?']").val() == "Yes") {
            hideALL = false;
      }
      else if ($("select[title='Service Lead Approved?']").val() == "No" && $("select[title='Funding Request Sanctioned?']").val() == "No") {
            okToShow = false;
      }
      else if ($("select[title='Service Lead Approved?']").val() == "Yes" && $("select[title='Funding Request Sanctioned?']").val() == "No") {
            okToShow = false;
      }
      // set up an array with the display names of the conditional fields
      var titles1 = ['Funding Request Sanctioned?'];
      var titles2 = ['Funding Request Sanctioned?','Service Lead Approved?'];
      for (var i = 0; i < titles1.length; i++) {
            nobr.filter(":contains('" + titles1[i] + "')").closest("tr").toggle(okToShow);
      }
      for (var i = 0; i < titles2.length; i++) {
            nobr.filter(":contains('" + titles2[i] + "')").closest("tr").toggle(hideALL);
      }
}
$(document).ready(function() {
      // initialize global vars
      nobr = $("nobr");
      showConditionalRows();
      $("select[title='Service Lead Approved?'], select[title='Funding Request Sanctioned?']").change(function() {
            showConditionalRows();
      });
});



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with hiding 2 SharePoint fields with javascript/jQuery

#2 FerretHolmes   User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Re: Problem with hiding 2 SharePoint fields with javascript/jQuery

Posted 07 March 2014 - 07:39 AM

You only need one if statement to check if both are true since you only have two possible outcomes in your code, true and false.
if ( something == true && somethingElse == true ) {
  okToShow = true;
} else {
  okToShow = false;
}


Not entirely sure this will correct your errors, but it was something I noticed.

This post has been edited by FerretHolmes: 07 March 2014 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 stylus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-May 13

Re: Problem with hiding 2 SharePoint fields with javascript/jQuery

Posted 07 March 2014 - 07:57 AM

I have 3 possible outcomes, not 2. That is the issue.

If both fields = YES then both fields should be hidden (Outcome 1)
If field A = YES and field B = NO, one field should be hidden (Outcome 2)
If field A = NO and field B = YES, one field should be hidden (Outcome 3)

My issue is that Outcome 2 and 3 work, but Outcome 1 only hides 1 of the fields I want to hide!

This post has been edited by Dormilich: 07 March 2014 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 FerretHolmes   User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Re: Problem with hiding 2 SharePoint fields with javascript/jQuery

Posted 07 March 2014 - 11:10 AM

I see, my mistake. I wasn't paying enough attention to your loops :whistling: Why couldn't something like this work? Or am I missing a bigger picture?

function showConditionalFields() {
var sla = $("select[title='Service Lead Approved?']");
var frs = $("select[title='Funding Request Sanctioned?']");

sla.show();
frs.show();

if ( sla.val() == "Yes" ) { 
   sla.hide();
} else if ( frs.val() == "Yes" {
   frs.hide();
}
}


Was This Post Helpful? 0
  • +
  • -

#5 stylus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-May 13

Re: Problem with hiding 2 SharePoint fields with javascript/jQuery

Posted 10 March 2014 - 04:19 AM

View PostFerretHolmes, on 07 March 2014 - 11:10 AM, said:

I see, my mistake. I wasn't paying enough attention to your loops :whistling:/> Why couldn't something like this work? Or am I missing a bigger picture?

function showConditionalFields() {
var sla = $("select[title='Service Lead Approved?']");
var frs = $("select[title='Funding Request Sanctioned?']");

sla.show();
frs.show();

if ( sla.val() == "Yes" ) { 
   sla.hide();
} else if ( frs.val() == "Yes" {
   frs.hide();
}
}



Thanks for the reply however this still only hides one field.

If SLA = 'Yes' then it is hidden
If FRS = 'Yes' it is hidden

But I need them BOTH to be hidden if both = 'Yes'

My code works BUT when both fields equal 'Yes' only one field is hidden, but my array stipulates both fields. I dont understand why the other field isnt being hidden!
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Problem with hiding 2 SharePoint fields with javascript/jQuery

Posted 11 March 2014 - 10:50 AM

Because you have an else. Remove the else so that you just have 2 if statements. With the else, if the first if statement succeeds then the second one does not get executed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1