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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

how to show/hide a tab using javascript

(0) ShareShare
ReportReport
Posted on by 102

I know this has been asked a million times but none of the jscripts have worked for me. 

(I'm a beginner)

I want to hide a tab and all sections within it. This will be based on an option set where I have edited the 'yes' and 'no'. 

The tab is called: Threatproducts

The field is called: opportunityorthreat

The tab has four sections: do I need to also include hiding these when I write the code or will hiding the tab they're in suffice?

I want it to hide when the field (on a different tab) is set to either 'yes' or 'no'. Would these be set to 'true' or 'false' in the code?

I have the same question (0)
  • meelamri Profile Picture
    13,216 User Group Leader on at
    RE: how to show/hide a tab using javascript

    Hi, 

    Please share your code...we Will fixe the issue together

  • ydynamics Profile Picture
    102 on at
    RE: how to show/hide a tab using javascript

    Hi Mehdi,

    Thank you so much for getting back to me! Here's my code (I think it's the 100th version of it! )

    function DisplayState() {

    var vent_py3_opportunityorthreat =Xrm.Page.getAttribute("vent_py3_opportunityorthreat").getValue();

    if (vent_py3_opportunityorthreat == false) {

    formContext.ui.tabs.get("tabThreatcgproducts").setDisplayState(true);

    formContext.ui.tabs.get("tabThreatcgproducts").setDisplayState(false);

    }

    else if (vent_py3_opportunityorthreat == true) {

    formContext.ui.tabs.get("tabThreatcgproducts").setDisplayState(true);

    formContext.ui.tabs.get("tabThreatcgproducts").setDisplayState(false);

    }

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: how to show/hide a tab using javascript

    Hello,

    You either define the formContext or use the following code:

    function DisplayState() {

    var vent_py3_opportunityorthreat =Xrm.Page.getAttribute("vent_py3_opportunityorthreat").getValue();

    if (vent_py3_opportunityorthreat == false) {

    Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(true);

    Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(false);

    }

    else if (vent_py3_opportunityorthreat == true) {

    Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(true);

    Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(false);

    }

  • ydynamics Profile Picture
    102 on at
    RE: how to show/hide a tab using javascript

    Thank you Andrew, I've just tried it and got the following error:

    Web resource method does not exist: DisplayState

    Session Id: 9d990f5b-a73e-47fc-8407-199f8b7a42df

    Correlation Id: 5ab15d85-9ea3-47ed-b469-e133610cef12

    Event Name: onsave

    Function Name: DisplayState

    Web Resource Name: cg_hide_threats_tab

    Solution Name: Active

    Publisher Name: DefaultPublishercityandguilds

    Time: Wed Jun 16 2021 22:10:21 GMT+0100 (British Summer Time)

    Do you think it's something to do with the way I've entered details on the form in 'Event Handler'?

    pastedimage1623878238472v2.png

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: how to show/hide a tab using javascript

    Add a closing curly bracket to the end of the script

    }

  • ydynamics Profile Picture
    102 on at
    RE: how to show/hide a tab using javascript

    Morning Andrew,

    I do have a bracket at the end; is it in the wrong place?

    function DisplayState() {

    var vent_py3_opportunityorthreat =Xrm.Page.getAttribute("vent_py3_opportunityorthreat").getValue();

    if (vent_py3_opportunityorthreat == false) {

    Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(true);

    Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(false);

    }

    else if (vent_py3_opportunityorthreat == true) {

    Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(true);

    Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(false);

    }

  • meelamri Profile Picture
    13,216 User Group Leader on at
    RE: how to show/hide a tab using javascript

    Hi, 

    I fixed the code for you:

    function DisplayState() {
        var vent_py3_opportunityorthreat = Xrm.Page.getAttribute("vent_py3_opportunityorthreat").getValue();
        if (vent_py3_opportunityorthreat == false) {
            Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(true);
            Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(false);
        }
        else if (vent_py3_opportunityorthreat == true) {
            Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(true);
            Xrm.Page.ui.tabs.get("tabThreatcgproducts").setDisplayState(false);
        }
    }

    If it works for you, please verify Andrew's answer. 

  • ydynamics Profile Picture
    102 on at
    RE: how to show/hide a tab using javascript

    Hi Mehdi,

    Thank you so much for doing this but I now get this error:

    Cannot read property 'getValue' of null

    Session Id: 1b90141f-538f-452d-931f-513d9405eb2f

    Correlation Id: 54e23442-e5b8-4431-a243-8b157c4128db

    Event Name: onsave

    Function Name: DisplayState

    Web Resource Name: cg_hide_threats_tab

    Solution Name: Active

    Publisher Name: DefaultPublishercityandguilds

    Time: Thu Jun 17 2021 09:48:42 GMT+0100 (British Summer Time)

  • meelamri Profile Picture
    13,216 User Group Leader on at
    RE: how to show/hide a tab using javascript

    All right, no worries. Let's start from the beginning...Share some screenshots explaining exactly what you want to achieve. Also, share the name of your tabs and sections

  • Charan Raju C R Profile Picture
    7 Moderator on at
    RE: how to show/hide a tab using javascript

    Hi ydynamics,

    Looks like the field vent_py3_opportunityorthreat is not placed on the form you have registered onsave javascript function.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 149

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 132

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 123 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans