Tuesday, March 26, 2013

CRM 2011 Set focus on email body javascript

I had an request that after email form is loaded I needed to set the focus on body field and not on Subject field like crm is doing.
Note that this is an unsupported customization so it might not work for you. You'll need jquery to be included (I used jquery-1.8.3.min).


function SetFocusOnMailBody() {
    var ifrm = document.getElementById("descriptionIFrame");
    ifrm.focus();

    var contentbody = $(ifrm).contents().find("body");
    var char = 0, sel; // character at which to place caret
    contentbody.focus();
    if (document.selection) {
        sel = document.selection.createRange();
        sel.moveStart('character', char);
        sel.select();
    }
}

I called this method after I was setting some fields in Email form at Onload.
The only way I could make it work was to put a timeout (because I could't figure out from where the focus is automatically set on subject field).


setTimeout(function () {
                //set focus on mail body
                SetFocusOnMailBody();
            }, 100);

Hope this helps.

No comments:

Post a Comment