web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Hide form field based on selection on previous tab selection on a form

(0) ShareShare
ReportReport
Posted on by 15

I have an entity form with multiple tabs that are accessed using the next button. The jQuery code I have created works if the field is on the same tab but it is not working when it's assigned to a field on a different tab. The question is "if question 11 (current tab) dropdown select is selected as no then hide question 19 (next tab)" othewise show question 19. 

This is my code, which works fine if the questions are to be seen/hidden are on the same tab. Any solutions?

//if Q11 = No hide Q19
$(document).ready(function() {

  $("#ahitask_brop04seccq11").change(SetFieldsVisibilityQ11b);
  $("#ahitask_brop04seccq11").change();
});

function SetFieldsVisibilityQ11b() {
  var selectedValue = $("#ahitask_brop04seccq11").val();

  if (selectedValue == "352230001")
  {
    $("#ahitask_brop05secdq19_label").closest("tr").hide();
    $("#ahitask_brop05secdq19").prop("required",false).closest("tr").hide();
  }
  else
  {
    $("#ahitask_brop05secdq19_label").closest("tr").show();
    $("#ahitask_brop05secdq19").prop("required",true).closest("tr").show();
  }
}
I have the same question (0)
  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Hi

    Your script seems to access dom elements directly and it is not a supported way of hiding/showing fields or tabs.

    Please use the Dynamics 365 client-side libraries to show/hide fields or tabs.

    Since you are using Dynamics 365 Online, I would recommend you use formcontext.getControl to get hold of the attribute and then use setVisible function to hide or show. Please see below

    Please visit the links below to know more about out of the box client-side libraries

    docs.microsoft.com/.../clientapi-form-context

    You can get get the formcontext using the following line

    var formContext = executionContext.getFormContext(); // get formContext

    Please refer to the link below to know more about passing execution context

    carldesouza.com/.../

    Once you got the formcontext, you can show / hide fields, tabs or sections as shown below

    To show/hide a field : formContext.getControl("fieldname").setVisible(true / false);  // true or false to hide or show

    To show/hide a tab : formContext.ui.tabs.get(tabName).setVisible(true/false)

    To show/hide a section : formContext.ui.tabs.get(tabName).sections.get(sectionName).setVisible(true/false);

    To hide/show a field in a particular section in a tab : formContext.ui.tabs.get(tabName).sections.get(sectionName).controls.get("fieldname").setVisible(true/false)

    Please note that you do not need any additional javascript libraries to do this.

    Some useful links

    docs.microsoft.com/.../formcontext-ui-tabs

    docs.microsoft.com/.../controls

    Hope this helps

  • Suggested answer
    Johao Larios Profile Picture
    1,795 on at

    Hello there,

    As mentioned by Kokulan, your script is using not supported methods.  docs.microsoft.com/.../customizations-supported

    You should be using the formContext to interact with the form.  docs.microsoft.com/.../clientapi-form-context

    Our recommendation is re-factor your code to use supported ways to interact with the form, kokulan has provided the functions needed for your requirement.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans