Recent searches


No recent searches

How to hide dropdown fields based on dropdown selection?

Answered


Posted Apr 03, 2023

Hi, 

How can we use custom code to make request form fields visible or invisible for end users based on dropdown selection? 


0

12

12 comments

image avatar

Jahn

Zendesk LuminaryCommunity Moderator

Hi Johnny J, you can actually create condition ticket field/s (customer facing or agent facing) based on the prior selection of the drop down.

This article might help you with this - https://support.zendesk.com/hc/en-us/articles/4408834799770-Creating-conditional-ticket-fields 


0


Hey Jahn,

Thanks for your suggestion, creating conditional fields won't work with my request. When users choose a particular dropdown value from a dropdown menu, I want to make a certain field invisible to them. Fields ought to appear when a user chooses an alternative dropdown. Currently, when  user selects another drop-down option, the conditional fields totally hide all of the fields for them. So, using custom code will enable me to hide a certain field from the user based on their selection.

Just an example, there is a dropdown called State and the user chooses Georgia. Only certain fields should be visible upon selection, and the conditional fields hides all required field when the user chooses any other state than Georgia.

 

0


image avatar

Arianne Batiles

Zendesk Customer Care

Hi Johnny,

Using conditional ticket fields can hide/display whole fields, but not options within a single fieldSome customers have done custom coding to their Help Centers to change the appearance for end-users. Unfortunately, queries regarding custom coding and troubleshooting issues related to the customizations you have made are outside the range of the support we are able to offer. 

We have an app that can hide field options of a drop-down field which is called Ticket Field Manager App. The app allows you to hide options for a given dropdown field. However, this is only applicable in the agent interface and not to the request form. See Installing and using the Ticket Field Manager app for more details. 

0


Hey Arianne Batiles,

Thanks for your response, I understand that queries about custom coding fall outside of your support's scope.  But, in the past I've seen and had others assist us with the code, so I thought I'd try and see if anyone could assist with the custom code:)

 

0


image avatar

Arianne Batiles

Zendesk Customer Care

Hi Johnny, 

Unfortunately, the previous community post has been archived. You might have some luck with this 3rd-party app called Formset since it allows customers to set the visibility of certain field options as well as it claims to work for both the agent and end-user experience. 

0


Thank you Arianne Batiles

Hey @Ahmad Zaid, Thank you, I'll have a look at it, but is the group only for developers?

 

0


image avatar

Jakub

Zendesk Customer Care

Hello Johnny, if you happen to still need some assistance with this, I was able to hide certain ticket fields based on the selection in another field, this involves some if statements and dom observers, but can be achieved. Let me know if you would like me to share this workaround. Have a good day! 

0


Hello Kuba, yes, it's great to hear that you've found a workaround. Appreciate your willingness to share this solution. Please do share the details when you have a moment, it could be very helpful for our team. Thank you.

0


image avatar

Jakub

Zendesk Customer Care

First, you will need to hide the fields that you want to show later on based on the selection in your dropdown field. In my case, I am using the code for both subject and description fields and hiding it with CSS (Customize > Edit code > styles.css)
 
#new_request > div.form-field.string.required.request_subject {
display: none;
}
 
#new_request > div.form-field.text.required.request_description {
display: none;
}


Then I apply the following code to show those fields based on a selection in my custom dropdown ticket field. 

PLEASE MAKE SURE TO CHANGE THE TICKET FIELD ID AND THE VALUE YOU WANT TO USE TO TRIGGER other fields to show up, I highlighted them in the code: 

function validateAndToggleUI() {
// Get the elements you want to hide
var subjectField = document.querySelector("#new_request > div.form-field.string.required.request_subject");
var descriptionField = document.querySelector("#new_request > div.form-field.text.required.request_description");

 
// Check if the value is "canyon"
if (request_custom_fields_9827560964756.value === "canyon") {
// If it's canyon, ensure the elements are visible
subjectField.style.display = "block";
descriptionField.style.display = "block";
} else {
// If not, hide the elements
subjectField.style.display = "none";
descriptionField.style.display = "none";
}
}

// Function to execute when mutations are observed
var observerCallback = function(mutationsList, observer) {
validateAndToggleUI();
};
 
// Create a MutationObserver instance to watch value changes on request_custom_fields_9827560964756
var observer = new MutationObserver(observerCallback);

// Assuming request_custom_fields_9827560964756 is the actual JS object
observer.observe(request_custom_fields_9827560964756, { attributes: true, attributeFilter: ['value'] });



0


Hello Kuba, thank you for sharing. It worked perfectly for me. I appreciate your assistance:)

I was wondering if it's possible to hide a specific value within a dropdown using a similar approach. For instance, consider dropdown A with two values: value 1 and value 2. Simultaneously, there's another dropdown, let's call it dropdown B, with three values: value x, value y, and value z.

When value 1 is selected from dropdown A, dropdown B should present all three values - x, y, and z. However, when value 2 is chosen, dropdown B should only display two values - y and z. Is this possible?

0


@kuba would you possible have a solution for what @johnny J mentioned? Thanks!

0


Hi Johnny J Johnny J Noelle Cheng 

I posted about this solution in 2023 but here it is again, if you want to hide a specific value in a dropdown see below, im using a nesty panel.

Full solution here:

 

Finding the ID in your HTML (Step 1)

First find the name of the dropdown ID. 

To find the IDs of the options, you'll need to enable "Emulate a focused page" in the console. Here's how:

  1. Open the console in Chrome.
  2. Click the three dots in the top right corner.
  3. Go to "More tools" > "Rendering."
  4. Click on "Emulate a focused page."

A screenshot of a computer

Description automatically generated

Once you activate "Emulate a focused page," you can open the dropdown, and it will remain open, allowing you to hover over the options and view their IDs.

My ID on the dropdown was called ‘new_application_form'

--------------------

 

Adding code to script.js file (Step 2)

Now paste this code below into the bottom of your script.js file, In my case I have my knowledgebase open to the public, so anonymous can view it, in this example I want to hide this dropdown option from all users in my organization, these are Anonymous, Staff, Students. You can check yours under the admin portal – people – configuration – tags.. I could also just restrict one or two groups if I wanted. 

Note, all you need to do is Replace the line ‘new_application_form’ with your dropdown ID. Apart from that paste is as is below. 

// For this funtion to work i needed to call it in my script.js file first.
$(document).ready(function() {
  var hideoption = document.getElementById('new_application_form');
  if (HelpCenter.user.role == "anonymous" || HelpCenter.user.organizations[0].name == "Staff" || HelpCenter.user.organizations[0].name == "Student") {
    if (hideoption !== null) {
      hideoption.style.display = 'none';
    }
    var optionsToRemove = ['new_application_form'];
    hideDropdownOptions(optionsToRemove);
  }
});

// This will hide the option with the id demo_field if the user is anonymous.
function hideDropdownOptions(tagsToRemove) {
  if (tagsToRemove) {
    Array.prototype.forEach.call(document.querySelectorAll(".nesty-panel"), function(tagsElement) {
      if (tagsElement !== null) {
        var fieldDisableTagsObserver = new MutationObserver(function(mutations) {
          mutations.forEach(function(mutation) {
            if (mutation.type == 'childList') {
              if (mutation.addedNodes.length >= 1) {
                for (var i = 0; i < mutation.addedNodes.length; i++) {
                  for (var j in tagsToRemove) {
                    var tagElementToRemove = document.querySelector("li#" + tagsToRemove[j]);
                    if (tagElementToRemove !== null) {
                      tagElementToRemove.remove();
                    }
                  }          
                }    
              }
            }
          });
        });
        fieldDisableTagsObserver.observe(tagsElement, {childList: true});
      }
    });
  } 
}    

 

This function checks if the user is anonymous. If so, it iterates over all elements with the class nesty-panel and sets up a MutationObserver to watch for changes in the DOM within these elements. When nodes are added to the DOM, it checks if the option with the id standard_request exists and removes it if found.

Make sure that:

  • The HelpCenter.user.role correctly reflects the user's role.
  • The class nesty-panel accurately identifies the elements containing the dropdown.
  • The ID new_application_form matches the ID of the option you want to hide.

If everything is set up correctly, this code should hide the new_application_form option for anonymous users and all signed in org users.

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post