Skip to main content
Community site session details

Community site session details

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

Marketingpage OnChange event lookup Marketingform

(0) ShareShare
ReportReport
Posted on by 34

Hi all,

I'm trying to create a script for the scenario in which when a lookup field on a marketingform (hosted on D365 MPage) is set an OnChange kind of script should trigger with a validation.
After trying myself I keep getting stuck on the fact that the script won't trigger on a select (however it will upon manual input).

As I am pretty new to the coding side can anybody give me some direction?

<script>
MsCrmMkt.MsCrmFormLoader.on('afterFormLoad', function (event) {

const Course = document.getElementById('79f74c1e-1c0f-ed11-82e4-000d3a2dc796');
Course.addEventListener('oninput', (event) => {});

oninput = (event) => {

if (event.target.value.includes ('XYZ')) {
//enable checkbox
document.getElementById('705944c9-f8c7-ec11-a7b5-000d3a233ba7').disabled = false;
}
else {
//disable checkbox
document.getElementById('705944c9-f8c7-ec11-a7b5-000d3a233ba7').disabled = true;
}
};
});
</script>

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Marketingpage OnChange event lookup Marketingform

    Hi Stevie_nld,

    Sorry for late replying. I tested this on my side. The onchange event would not be triggered too. The reason for this is that when you select another option in the Lookup dropdown list, the input's value would not be changed, it changes the 'data-value' property of the input:

    pastedimage1659943530628v1.png

    So you would monitor the change of this property, here is my test code, you could have a try:

    <script>
    MsCrmMkt.MsCrmFormLoader.on('afterFormLoad', function (event) {

    const Course = document.getElementById('58654b56-e216-ed11-b83e-000d3a5bc6b1');
    console.log(Course);
    var observer = new MutationObserver(function(mutations) {
    console.log('data-value changed');
    //your code
    });
    observer.observe(Course, {
    attributes: true,
    attributeFilter: ['data-value'] });
    });
    </script>
    Result:
    Onchange.gif
    BTW, 'blur' can be a workaround. However, it's not perfect. Because it would be trigger when you select the same value twice.
  • Stevie_nld Profile Picture
    34 on at
    RE: Marketingpage OnChange event lookup Marketingform

    I found a working solution....not sure if it's a nice one but hey....

    I used blur and not change, and that did the trick.

    So

    Course.addEventListener('blur', (event) => {});

  • Stevie_nld Profile Picture
    34 on at
    RE: Marketingpage OnChange event lookup Marketingform

    Hi Steve Zhao,

    Thanks for your reply!

    When using the OnChange, it does not seem to register all changes to the field.

    When I select a value, the script is not triggered (tested by adding an alert).

    Only after I add a <space> or whatever in the field after selecting it will trigger.

    Any idea how to fix this?

    Kinds Regards,

    Stevie

  • Community Member Profile Picture
    on at
    RE: Marketingpage OnChange event lookup Marketingform

    Hi Stevie_nld,

    'oninput' event occurs when an element gets user input. Please try to change your event to 'onchange' to have a try.

    It should trigger after you change the lookup value.

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

News and Announcements

Now Available: 2025 Release Wave 2

Quick Links

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June 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 > Customer experience | Sales, Customer Insights, CRM

#1
Holly Huffman Profile Picture

Holly Huffman 103

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 96 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

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

Product updates

Dynamics 365 release plans