views:

582

answers:

2

I have the following code in the OnChange() event for a field.

alert("alert text");
crmForm.all.fieldname.SetFocus();

The page acts like the SetFocus call isn't even there.

Anyone know why this is?

EDIT: I've also tried the following to no avail.

crmForm.all.fieldname.Focus();
crmForm.all.fieldname.focus();
alert("alert text", function() { crmForm.all.fieldname.SetFocus()});
+3  A: 

In the DOM, the function to set focus on an element is called focus(), not SetFocus().

Tomalak
This may be correct for javascript outside of MS CRM but this does not work for the OnChange event within MS CRM.
mwright
from your code sample, is `crmForm.all.fieldname` a reference to your field in the DOM? If it is, it should work, if not, what is it?
scunliffe
the "fieldname" is the name of the attribute associated with the entity. I would assume that when it is loaded on the form that is the name of the DOM element as well, however, when modified to use the suggested focus() after the alert the behavior did not change.
mwright
From some other reading it appears that Microsoft overrides the focus event within CRM which is why this doesn't work.
mwright
@mwright: Sorry for my ignorance in the finer details of MS CRM ;)
Tomalak
@Tomalak: I appreciate the effort.
mwright
+1  A: 

Turns out that retaining focus on the field from which the OnChange() method was called is broken in CRM 4 without the most recent rollup. This is a known issue with a Microsoft KB article.

To achieve the illusion of retaining focus on the field simply set the focus to a different field on the same tab first and then reassign the focus to the field from which the OnChange() event was called like so:

alert("alert text");
crmForm.all.some_other_field_on_the_same_tab.SetFocus();
crmForm.all.fieldname.SetFocus();
mwright
+1 for finding out and posting the answer back here! You can accept your own answer after 48 hours. I'll make mine a community wiki and let it sit there for reference.
Tomalak