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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Dynamic Forms based on...
Power Pages
Answered

Dynamic Forms based on dropdown with multi-select using custom JavaScript

(0) ShareShare
ReportReport
Posted on by

I am trying to customize a form where I would like to show/hide fields based on what choices the user has selected. 

 

E.g. Dropdown values are A, B, C

If user selects A and B, show controls X (related to A) and Y, Z (related to B)

If user selects only A, show control X. Similarly, if user selects only B, show controls Y and Z.

 

I am able to do that using a regular dropdown (where multi-select disabled) and with radio buttons as well using the custom JavaScript section on the form. 

 

Can this be done using JavaScript or are there any alternatives? Thanks in advance!

Categories:
I have the same question (0)
  • Atmaram90 Profile Picture
    126 on at
  • SS-07051451-0 Profile Picture
    on at

    Hi, I tried this and I am unable to call the function when it the dropdown has the multi-select option enabled. The same change function works when I configured it for a different form where the the dropdown has multi-select option disabled.

     

    Here is the custom JavaScript I have on the form: 

     $(document).ready(function () {
     $("#column_name").on("change",onDropDownChange);
     onDropDownChange();
    
     $('#column_name').change(function()
     {
     console.log("NEW FUNCTION: CALL");
     //Gets the text value of the selected option.
     var selectedOption = $("#column_name")[0].selectedOptions[0].innerHTML;
     
     if(selectedOption === "Option 1"){
     $('#input1_column_name').closest("td").find("div.control, div.info").show(); 
     }else if(selectedOption === "Option 2")
     {
     $('#input2_column_name1').closest("td").find("div.control, div.info").show();
     $('#input2_column_name2').closest("td").find("div.control, div.info").show(); 
     }
     })
     });
    
     function onDropDownChange() {
     var selectedOptions = $('#column_name').find("option:selected").text();
     
     // Display selected values in console
     console.log("Inside the function call!");
     console.log("Selected options:", selectedOptions);
    
     // Hide all controls first
     $('#input1_column_name').closest("td").find("div.control, div.info").hide(); 
     $('#input2_column_name1').closest("td").find("div.control, div.info").hide();
     $('#input2_column_name2').closest("td").find("div.control, div.info").hide();
     
     if (selectedOptions === 'Option 1') {
     $('#input1_column_name').closest("td").find("div.control, div.info").show(); 
     }
     
     if (selectedOptions === 'Option 2') {
     $('#input2_column_name1').closest("td").find("div.control, div.info").show();
     $('#input2_column_name2').closest("td").find("div.control, div.info").show(); 
     }
     }

     

    I understand that I am overwriting the function that is bound to the change event but neither function is being called as per the log. I also double checked the column's logical name that is being used. 

  • Verified answer
    Fubar Profile Picture
    8,361 Super User 2025 Season 2 on at

    The Mulitselect Choices on the Page is not a standard field, it is a PCF Control (a.k.a. code component), to capture a change you will need to use a mutation observer

    The following is from co-pilot and I haven't checked it it so it may not be 100% correct

     

    var fieldElement = document.getElementById('your_field_name');
    var observer = new MutationObserver(function(mutations) {
     console.log('Field value changed');
     // Handle the change event here
    });
    observer.observe(fieldElement, { attributes: true, attributeFilter: ['value'] });

     

    There is also this forum post https://powerusers.microsoft.com/t5/General-Discussions/Get-Value-of-Multi-Select-with-JavaScript/td-p/1618205

  • SS-07051451-0 Profile Picture
    on at

    Thank you! That helped, based off the code you provided and the article, I was able to get it working. Here is the template of the JavaScript for anyone trying to do the same.

     

     $(document).ready(function () {
     // Hide all controls first
     $('#control_1').closest("td").find("div.control, div.info").hide(); 
     $("#control_2").closest("td").find("div.control, div.info").hide();
     $("#control_3").closest("td").find("div.control, div.info").hide();
    
     // Hide all rows
     $('#control_1').closest("tr").toggle(false);
     $("#control_2").closest("tr").toggle(false);
     $("#control_3").closest("tr").toggle(false);
     
    
     var fieldElement = document.getElementById('drop_down');
     var observer = new MutationObserver(function(mutations) 
     {
     console.log('Field value changed');
     var referredbyarray = $("#drop_down").val();
     
     // Create trigger variables 
    			var showOption1Fields = false;
    			var showOption2Fields = false;
    			
    
     if (referredbyarray != "") 
     {
     // Debug Log Statement
     // console.log(referredbyarray);
     var referedByJSON = JSON.parse(referredbyarray);
     for (i = 0; i < referedByJSON.length; i++) 
     {
     if (referedByJSON[i].Value == "789650000")
     {
    						// Option 1
     showOption1Fields = true;
     }
     else if (referedByJSON[i].Value == "789650001")
     {
     // Option 2
    						showOption2Fields = true;
    
     }
     } 
    				
     // Show or hide fields based on trigger variables
     toggleFields(showOption1Fields, '#control_1');
     toggleFields(showOption2Fields, '#control_2', '#control_3');
     
     // Toggle visibility of entire rows based on trigger variables
     toggleRow(showOption1Fields, '#control_1');
     toggleRow(showOption2Fields, '#control_2', '#control_3');
     
     } 
     else 
     {
     // If no option is selected, hide all fields
     hideAllFieldsAndRows();
     }
     });
     observer.observe(fieldElement, { attributes: true, attributeFilter: ['value'] });
     });
    
    function toggleFields(show, ...fields) 
    {
     if (show) {
     fields.forEach(field => {
     $(field).closest("td").find("div.control, div.info").show();
     });
     } else {
     fields.forEach(field => {
     $(field).closest("td").find("div.control, div.info").hide();
     });
     }
    }
    
    function toggleRow(show, ...fields) 
    {
     if (show) {
     fields.forEach(field => {
     $(field).closest("tr").toggle(true);
     });
     } else {
     fields.forEach(field => {
     $(field).closest("tr").toggle(false);
     });
     }
    }
    
    function hideAllFieldsAndRows() {
     // Hide all fields and rows if no option is selected
     $('#control_1, #control_2, #control_3')
     .closest("td")
     .find("div.control, div.info")
     .hide();
     
     $('#control_1, #control_2, #control_3')
     .closest("tr")
     .toggle(false);
    }

     

  • CU26071520-1 Profile Picture
    20 on at

    Hello!

     

    Thanks so much for uploading the JavaScript that worked for you! Question - I am getting an error when I attempt to use this in Power Apps. If you used this JavaScript in Power Apps, could you please share what you named the function when you created an event handler? Additionally, did you only create an On Load event, or did you also create On Change events for the multi select field?

     

    I am newer to JavaScript and I have been trying to accomplish this all week! Any help is appreciated. Thank you!

  • SS-07051451-0 Profile Picture
    on at

    Hello! 

     

    I have a model driven application where few forms are used on a Power Pages Portal. The provided JavaScript is configured on the Portal Management. If you are trying to do the same on a Canvas app, take a look at the following article:  Show/hide fields conditionally in PowerApps forms based on dropdown selection – NateChamberlain.com

     

    This works when you are only selecting one value from the drop down. I'll have to search for articles that demonstrate the same for multi-select dropdowns. Hope that helps! 

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 78 Super User 2025 Season 2

#2
Jerry-IN Profile Picture

Jerry-IN 75

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard