Page 1 of 1

Conditionally show/hide fields on Display Form

Posted: 21 Sep 2017
by TonyDuke
I'm having a bit of an issue with conditionally showing and hiding fields on a display form. The test is if Address Line 1 is blank then hide items with CSS addressFields, this works absolutely fine in the edit form using the .value() code, but I cannot get it to work in the display form using .value() or the code below? The function is called on form load.

Code: Select all

function showAddressFields () {
	var AddressLine1 = fd.field('Address_x0020_Line_x0020_1').control()._el().text();
	if (AddressLine1 == "") {
		$('.addressFields').hide();
	}
}
Address Line 1 is a single line of text field.

Help appreciated.

Re: Conditionally show/hide fields on Display Form

Posted: 21 Sep 2017
by Nikita Kurguzov
The actual value returned by the empty field is most likely different. In my tests, the value returned for empty fields was a space. Try to compare value to " " instead of "".

Re: Conditionally show/hide fields on Display Form

Posted: 21 Sep 2017
by TonyDuke
Hi Nikita,

I did previously try that as you are correct in the display form if I get the value using the console it return " " but this doesn't work either, I've tried it a couple of other ways too -

Code: Select all

if (AddressLine1 != "") {
		$('.addressFields').show();
And then setting the fields to hidden by default, they just stay hidden, I've also tried

Code: Select all

if (!AddressLine1) {
		$('.addressFields').show();
Again with the fields hidden by default.

None of the above works, have you any other ideas I could try?

Thanks

Re: Conditionally show/hide fields on Display Form

Posted: 21 Sep 2017
by Nikita Kurguzov
Try this then:

Code: Select all

function showAddressFields() {
	var AddressLine1 = $.trim(fd.field('Address_x0020_Line_x0020_1').control()._el().text());
	if (AddressLine1 == "") {
		$('.addressFields').hide();
	}
}

Re: Conditionally show/hide fields on Display Form

Posted: 21 Sep 2017
by TonyDuke
Hi Nikita,

Yes, that's great thanks sorted the issue and the fields are now hidden if the address is empty.

Great stuff

Thanks