Sunday, 15 May 2011

How to set focus on a control in CRM 2011 using Javascript

How to set focus on a control in CRM 2011 using Javascript
 
Code Snippet: Event: Form Load
 
var control = Xrm.Page.ui.controls.get("AttributeName");

control.setFocus();

6 comments:

  1. Hi, i´m usign this section of code on OnChange event in my customize field, but it does not work, do you have ideas? i tried different lines of code but none of them work for me.

    thanks in advanced!

    ReplyDelete
  2. Hi Sergio,
    For onchange event its a bit different. Pls try the following.
    Pass the context from the onchange event of the field and then

    Xrm.Page.getControl(context.getEventSource().getName()).SetFocus(true);

    ReplyDelete
  3. Manjaly,
    Even this didn't worked for me.
    But got another work around which helped in my case.
    Set the focus to another control first and then to the control really u need to setFocus, as below.
    Xrm.Page.getControl("name").setFocus(true);
    Xrm.Page.getControl("TheFieldYouReallyWantToFocus").setFocus(true);

    For your reference the link is this. http://stackoverflow.com/questions/2684646/setting-field-focus-after-javascript-alert-not-working-in-ms-crm

    ReplyDelete
    Replies
    1. Use below code its working.
      document.getElementById("crmForm").setAttribute("setinitialfocus", false);
      Xrm.Page.getControl("TheFieldYouReallyWantToFocus").setFocus(true);


      Delete
  4. did any one know how to set focus in a lookup field

    ReplyDelete