Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Add event handler on field doesn't work

Posted on by Microsoft Employee

Hi everyone!

I want to add an onclick event handler on a field , so that when clicked (or focus) to hide an element.

Here is my code:

function setOnFocus()
{
    var preferredResource = document.getElementById("ral_preferredresource");
	
	if (preferredResource){
		alert("on click");
		preferredResource.attachEvent("onclick", hideFormAssistant);
	}
	
}



function hideFormAssistant()
{
  alert("in hideformassistant");
  document.getElementById('tdRelatedInformationPane').style.display = 'none';
}


Any ideas why it doesn't work? It errors on this line 

preferredResource.attachEvent("onclick", hideFormAssistant);

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Add event handler on field doesn't work

    Not sure if this can be done with supported way. Unfortunately the supported way most of the times is just not enough. But yes it is best practice to use it when you can!

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Add event handler on field doesn't work

    Shak,

    You use "Dom manipulations" that are not supported - that's why your code doesn't work as it expected ;) . I would suggest to find supported way to resolve your scenario.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Add event handler on field doesn't work

    Figured it out. Had to use addEventListener

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans