Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - General Discussions
Unanswered

How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

(2) ShareShare
ReportReport
Posted on by 6

Hello All,

 

I am trying to hide/show fields based on selected option in MultiSelect optionset but I am unable to trigger onchange event when an option is selected. I am using Advanced Forms in Power Apps portals. 

 

 

 

 $(document).ready(function () {
 $("#new_referredby").change(function () { alert("success"); });
 });

 

 

 

Categories:
  • reallyHots0ap Profile Picture
    4 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    @PriyankaKambleB -- Thank you so much! This worked for me as well. It was driving me nuts. 

  • FU Microsoft Profile Picture
    112 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    Your script works except when the last remaining selected item is removed, then the event is not trapped.

  • PriyankaKambleB Profile Picture
    9 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    if (window.jQuery) {
    (function ($) {
    $(document).ready(function () {
    $("#bit_other").closest("td").find("div.control, div.info").hide();


    var foo = document.getElementById('bit_services'); // specify multiselect option set field
    var observer = new MutationObserver(function (mutations) {
    var services = $("#bit_services").val(); // specify multiselect option set field
    var showOther = false; // declaring boolean
    if (services != "") { //if your multiselect option has at least 1 value selected
    var servicesJSONObj = JSON.parse(services); //parse multiselect value in json format
    for (i = 0; i < servicesJSONObj.length; i++) {
    alert(servicesJSONObj[i].Value);
    if (servicesJSONObj[i].Value == "757730009") //if specific value is selected in multiselect field
    showOther = true; //then show the field
    }
    } else
    showOther = false;

    if (showOther == true) {
    $("#bit_other").closest("td").find("div.control, div.info").show(); //show other field
    } else {
    $("#bit_other").val("");
    $("#bit_other").closest("td").find("div.control, div.info").hide(); //hide other field
    }
    });
    observer.observe(foo, {
    attributes: true,
    attributeFilter: ['value']
    });
    });
    }(window.jQuery));
    }

    this is working for me

  • PriyankaKambleB Profile Picture
    9 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    This has helped for me. Further to elaborate this function, i am hiding bit_other field based on my multiselect option set field bit_services. Here is the detailed code:

    if (window.jQuery) {
    (function ($) {
    $(document).ready(function () {
    $("#bit_other").closest("td").find("div.control, div.info").hide();


    var foo = document.getElementById('bit_services'); // specify multiselect option set field
    var observer = new MutationObserver(function (mutations) {
    var services = $("#bit_services").val(); // specify multiselect option set field
    var showOther = false; // declaring boolean
    if (services != "") { //if your multiselect option has at least 1 value selected
    var servicesJSONObj = JSON.parse(services); //parse multiselect value in json format
    for (i = 0; i < servicesJSONObj.length; i++) {
    alert(servicesJSONObj[i].Value);
    if (servicesJSONObj[i].Value == "757730009") //if specific value is selected in multiselect field
    showOther = true; //then show the field
    }
    } else
    showOther = false;

    if (showOther == true) {
    $("#bit_other").closest("td").find("div.control, div.info").show(); //show other field
    } else {
    $("#bit_other").val("");
    $("#bit_other").closest("td").find("div.control, div.info").hide(); //hide other field
    }
    });
    observer.observe(foo, {
    attributes: true,
    attributeFilter: ['value']
    });
    });
    }(window.jQuery));
    }

  • panug Profile Picture
    19 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    @pankajErrolla1 
    Try below sample. This worked for me.
    Find element id similar to " *_ledit".

     

    $(document).ready(function(){
    // Function to handle the mutation event
    function handleMutation(mutationsList) {
    mutationsList.forEach(function(mutation) {
    if (mutation.type === 'attributes' && mutation.attributeName === 'aria-label') {
    // The 'type' attribute of the input element has changed
    alert('Input type attribute has changed to: ' + mutation.target.getAttribute('aria-label'));
    }
    });
    }

    // Create a MutationObserver
    var observer = new MutationObserver(handleMutation);

    // Define the target node (the input element)
    var targetNode = document.getElementById('country_ledit');

    // Configure the observer to watch for attribute changes
    var config = { attributes: true, attributeFilter: ['aria-label'] };

    // Start observing the target node for mutations
    observer.observe(targetNode, config);

    });
  • pankajErrolla1 Profile Picture
    2 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    Team I have tried the above piece of code to fire JavaScript on change of option value in my multiselectoptionset. Can you please confirm if the above js is working fine or not  ? I would be really helpful if you can share the mutation observer full code.

     

    Also @kerridge252  i have tried your code too but no luck.. 

  • Fubar Profile Picture
    7,960 Super User 2025 Season 1 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    You can do it with a mutation observer, when a value is selected/deselected in the multi-select the value attribute gets updated.

    var foo = document.getElementById('fieldname');
    
    var observer = new MutationObserver(function(mutations) {
     console.log('changed');
    });
    observer.observe(foo, { 
     attributes: true, 
     attributeFilter: ['value'] });

     

  • kerridge252 Profile Picture
    31 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    I got this working by keeping track of the "container" html element title attribute. And then adding an on change to that html element as well. 

     

    original code:


    theOldText = $(".msos-selecteditems-container").attr('title');

     

    $(".msos-selecteditems-container").on('DOMSubtreeModified', function(e) {

        var theText = $(".msos-selecteditems-container").attr('title');

        if(theText !== theOldText){

            console.log(theText);

            theOldText = $(".msos-selecteditems-container").attr('title');

        }

    });

    kerridge252_0-1663229041253.png

     

    guessing yours's will be:

    theOldText = $(".new_referredby-container").attr('title');

     

    $(".new_referredby-container").on('DOMSubtreeModified', function(e) {

        var theText = $(".new_referredby-container").attr('title');

        if(theText !== theOldText){

            console.log(theText);

            theOldText = $(".new_referredby-container").attr('title');

        }

    });

     

  • abm abm Profile Picture
    32,472 Most Valuable Professional on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    Hi @Jyo17 

     

    Try the below

     

    $(document).ready(function () {
          document.getElementById("new_referredby").addEventListener("change", referredbyFunction);

          function referredbyFunction() {
                   var x = document.getElementById("new_referredby");
                   alert("success");
           }
    }

  • Jyo17 Profile Picture
    6 on at
    Re: How to trigger on change event on MultiSelect Option Set in PowerApps Portals using Javascript

    Hello Ragavanrajan,

     

    Thank you for your reply. I have tried it already but it didn't work. 

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

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 69 Super User 2025 Season 1

#2
oliver.rodrigues Profile Picture

oliver.rodrigues 49 Most Valuable Professional

#3
Jon Unzueta Profile Picture

Jon Unzueta 43