locked
Simple onclick event help-- CRM 2011 JScript RRS feed

  • Question

  • Does anyone know a really simple way to make this work?

    OnLoad() {
    //crmForm.all.new_name.onclick=function() {
    Xrm.Page.ui.controls.new_name.onclick = function() {
    //Xrm.Page.new_name.onclick = function() {
        alert(‘Clicked’);
        }
    }
    

    The SDK uses this example, but I can't seem to get it to work either.  ><  

    window.attachEvent("onresize", function () {
      SDK.UISamples.updateViewPortSize(viewPortDisplay);
     });
    
    
    


    Friday, October 14, 2011 8:51 PM

Answers

  • function OnLoad()
    {
     crmForm.all.new_name.atachEvent("onkeypress", ClickAlert);
    }
    
    function ClickAlert()
    {
       alert("Clicked");
    }
    
    This worked fine for keystrokes, but not mouse clicks. 

    Sorry I forgot to specify the right event, change the event "onkeypress" to "onclick".. this should work. I also have a typo in the Attach Event command, here is the correct one:

     

    function OnLoad()
    {
     crmForm.all.new_name.attachEvent("onclick", ClickAlert);
    }
    
    function ClickAlert()
    {
       alert("Clicked");
    }

    I hope this helps you.

    • Marked as answer by Pvt. Stash Monday, October 17, 2011 4:08 PM
    Monday, October 17, 2011 3:40 PM

All replies

  • I am a bit curious.  Can you tell me where the SDK references that example?
    Jamie Miley
    Check out my about.me profile!
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Friday, October 14, 2011 9:20 PM
    Moderator
  • I am a bit curious.  Can you tell me where the SDK references that example?

    Search for 'attachevent' in the CRM 2011 SDK.  The two results should be 'Xrm.Page.ui' and 'Sample.SDKUISamples.js'.  A text search for 'window' should get you to the lines I'm referencing.  

    I actually did get the onmouseenter and onmouseout events to work using the following code:

    function load() {
        var input = document.getElementById("textbox");
        input.attachEvent("onmouseenter", function () {
            document.getElementById("texbox").value = "Hello World!";
        });
    
        input.attachEvent("onmouseout", function () {
            document.getElementById("textbox").value = "";
        });
    }
    
    window.onload = load;
    

     

    I still can't get an onclick event to work, however.  I've been working on building a multi-select Option Set, and because I haven't gotten the bulk code from several blogs to work I've been building it up a piece at a time.  At this point I have a multi-select pick list like in James OConnor's blog.  This list doesn't collapse, however, so I'd like to make it expand when the user clicks into it and collapse when the mouse leaves.  Xavier Vargas has pretty much the same code, but that one doesn't collapse either. 

    Any ideas?



    • Edited by Pvt. Stash Monday, October 17, 2011 1:52 PM
    Monday, October 17, 2011 1:51 PM
  • Try using this code:

    function OnLoad()
    {
     crmForm.all.new_name.atachEvent("onkeypress", ClickAlert);
    }
    
    function ClickAlert()
    {
       alert("Clicked");
    }
    


    Only have to add the OnLoad function in the form OnLoad event.

     

    I hope this helps.

    Regards,

    Frank

    Monday, October 17, 2011 2:38 PM
  • function OnLoad()
    {
     crmForm.all.new_name.atachEvent("onkeypress", ClickAlert);
    }
    
    function ClickAlert()
    {
       alert("Clicked");
    }
    
    This worked fine for keystrokes, but not mouse clicks. 

    • Edited by Pvt. Stash Monday, October 17, 2011 2:48 PM
    Monday, October 17, 2011 2:47 PM
  • function OnLoad()
    {
     crmForm.all.new_name.atachEvent("onkeypress", ClickAlert);
    }
    
    function ClickAlert()
    {
       alert("Clicked");
    }
    
    This worked fine for keystrokes, but not mouse clicks. 

    Sorry I forgot to specify the right event, change the event "onkeypress" to "onclick".. this should work. I also have a typo in the Attach Event command, here is the correct one:

     

    function OnLoad()
    {
     crmForm.all.new_name.attachEvent("onclick", ClickAlert);
    }
    
    function ClickAlert()
    {
       alert("Clicked");
    }

    I hope this helps you.

    • Marked as answer by Pvt. Stash Monday, October 17, 2011 4:08 PM
    Monday, October 17, 2011 3:40 PM
  • function OnLoad() { crmForm.all.new_name.attachEvent("onclick", ClickAlert); } function ClickAlert() { alert("Clicked"); }

    Yes it did!  I tried "onclick" over and over and for some reason it didn't work.  I copied your code and it did.  I must have had a typo somewhere that I kept missing.  Yay dyslexia! lol

    Thanks.

    Monday, October 17, 2011 4:07 PM
  • Even following code will work.

    function

     

    onLoad() {

     

    var object = document.getElementById('new_name'

    )

    object.onclick =

    function

    () {

    alert(

    'Clicked'

    );

    }

    }

    • Proposed as answer by PraveenK4 Tuesday, July 15, 2014 5:27 AM
    Thursday, November 10, 2011 10:09 AM
  • I know this is an old post, but for anyone else who may find it...

    This SDK sample uses attachEvent on the window object but never on one of the form controls. The difference is the scope. Every page has a global window object. CRM doesn't guarantee that you will be able to access the HTML element for the form control in the same way in future releases. You should always interact with form controls using the object returned by Xrm.Page.getControl().

    Some of the suggestions in this thread are not supported. I know, it is a bummer. In 2006 I wrote this blog post: http://blogs.msdn.com/b/crm/archive/2006/11/17/using-the-attachevent-method-to-show-users-context-sensitive-help.aspx and I was very quickly admonished with some good advice by a corresponding blog post http://blogs.msdn.com/b/crm/archive/2007/04/13/unsupported-scripting.aspx.

    I recommend reading the latter post.

    Sure, you can get it to work, but just remember that your customizations may not survive an upgrade (or perhaps even a update rollup).

    Even though the unsupported technique I blogged about back in CRM 3.0 probably still works in CRM 2011, at some point it will break.

    Perhaps someday the CRM Product will support click and mouseover events for form controls. Until then consider your requirements and whether you can achieve them using a supported method. If you have to go unsupported, document it and be sure to test it when things change.

    Make sure you can disable or remove any unsupported customizations before you contact support.

    Details about what is not supported is available at Supported Extensions for Microsoft Dynamics CRM.

    If you want a customized control on a form (like a multi-select picklist) I recommend you create an HTML web resource that provides the functionality you need (including the click and mouse events) and then add it to the form so you can wire it up to the parent.Xrm.Page object. You can use setVisible(false) on the control you want to replace.

    It is all about trying to 'future-proof' your customizations. Try to keep your code separate from CRM application code unless the CRM feature supports adding custom code to it.

    If you really want to have complete control of the UI - you can consider writing your own. The article Using Option Set Options with the REST Endpoint - JScript (Microsoft Dynamics CRM 2011) provides a partially implemented UI that allows for Create, Retrieve, Update, and Delete operations in an HTML Web Resource using the REST and SOAP endpoints.


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Sunday, February 5, 2012 10:11 PM
    Answerer
  • you have to attach event in this way

    function handler() {
           alert("Please Select ServiceLine"); }

    onload (){

     if (Xrm.Page.getAttribute("wipro_accountslid").getValue() == null) {
            //crmForm.all.wipro_practiceid.onclick = ClickHandlerwipro_practiceidwipro_accountslid;
            AddEvent(document.getElementById("wipro_practiceid"), 'onclick', handler);
        }

    }

    function AddEvent(html_element, event_name, event_function) {
        html_element.attachEvent(event_name, function () { event_function.call(html_element); });
        
    }

    Sunday, June 23, 2013 2:26 PM
  • Thanks bro. Its working fine
    Tuesday, July 15, 2014 5:26 AM