Two conditions inside an IF function

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

22 Apr 2017

Hi,

Im using the below script to hide / show a field:

Code: Select all

	 if (fd.field('ContentType0').control('getSelectedText') == 'Complaint') {
        $('div.fd_title').filter(function () { return $(this).text() === 'Root Cause (NCR/Complaint)' }).closest('tr').show();
		
		$('div.fd_title').filter(function () { return $(this).text() === 'Basic Root Causes' }).closest('tr').hide();
	}
    else {
        $('div.fd_title').filter(function () { return $(this).text() === 'Root Cause (NCR/Complaint)' }).closest('tr').hide();
		
		$('div.fd_title').filter(function () { return $(this).text() === 'Basic Root Causes' }).closest('tr').show();

    }
I would also like the script to run if the following condition also is true:

Code: Select all

 if (fd.field('ContentType0').control('getSelectedText') == 'Non-confomance')
How can I include two conditions inside the IF function?

Thanks in anticipation!

Vegard

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

24 Apr 2017

Hi,

You can join the conditions by using OR (||) operator:

Code: Select all

if (fd.field('ContentType0').control('getSelectedText') == 'Complaint' || 
    fd.field('ContentType0').control('getSelectedText') == 'Non-confomance') {...}
https://www.w3schools.com/js/js_comparisons.asp

User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

24 Apr 2017

Hi, Dmitry!

This works on the new form, but when loading the edit and display form both the fields ('Root Cause (NCR/Complaint)' and 'Basic Root Causes') are visible - see attachment.

Any ideas?

Vegard
Hide_show fields Forms Designer.PNG
Hide_show fields Forms Designer.PNG (27.79 KiB) Viewed 2595 times

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

25 Apr 2017

Please, note that the ContentType0 field must be located on the same form in order to check it's value via JavaScript. As for the display form, use the code below:

Code: Select all

if (fd.field('ContentType0').value() == 'Complaint' || 
    fd.field('ContentType0').value() == 'Non-confomance') {...}

User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

25 Apr 2017

Hi, again!

The 'ContentType0' is a visible on the form. The snip is only a small part of the form. I just noticed that when changing the value in the 'ContentType0' field (Lookup-field) which is a field that trigger some fields to hide/show after opening the editform the script is running as expected.

Any thoughts?

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

26 Apr 2017

Hi,
So, as far as I understood, the code is executed when a user switches the value but it also should be executed when a user opens the form, am I right?

User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

26 Apr 2017

Correct.

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

27 Apr 2017

OK, then just call the function containing your code outside of all handlers, ex:

Code: Select all

function myFunction() {
// you code with conditional hiding is here
}

// you call it inside a change handler:
fd.field('ContentType0').change(myFunction);

// you should also call it right here:
myFunction();

User avatar
vegard.grutle
Posts: 59
Joined: Fri Mar 17, 2017

28 Apr 2017

Thanks, I will try that!

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 6 guests