Asked by:
Focus field code not working

Question
-
Hello,
I'm using JavaScript on a form in CRM Dynamics 2011. On this instance I'm using the field focus function. I believe its all setup correctly and it does set to that field initially but because of the timing on the form it seems to load the rest later and selects another field.
Has anyone else had an issue with it not focusing on the right field when loading?
My code is this:
function FocusField()
{
Xrm.Page.getControl("new_focusfield").setFocus(true);
}Thanks
Wednesday, February 19, 2014 1:02 PM
All replies
-
--------------Try this------------
function FocusField()
{
var control = Xrm.Page.ui.controls.get("new_focusfield");
control.setFocus();
}
-------------------or-------------------
function SetFocusOnField(fieldName) {
if (fieldName != null) {
var control = Xrm.Page.ui.controls.get(fieldName);
control.setFocus();
}
}
Thanks in Advance Siva
Wednesday, February 19, 2014 1:13 PM -
--------------Try this------------
function FocusField()
{
var control = Xrm.Page.ui.controls.get("new_focusfield");
control.setFocus();
}
-------------------or-------------------
function SetFocusOnField(fieldName) {
if (fieldName != null) {
var control = Xrm.Page.ui.controls.get(fieldName);
control.setFocus();
}
}
Thanks in Advance Siva
Both of those codes did the same.
Once again the Script ran before the form opened properly.
Anyone got any ideas?
Wednesday, February 19, 2014 4:23 PM -
If the javascript is running before the form fully loads you can use timeout. Something like this
function timeout() { setTimeout(FocusField, 2000); } function FocusField() {
Xrm.Page.getControl("new_focusfield").setFocus(true);
}
- Edited by KawasakiRider03 Wednesday, February 19, 2014 4:38 PM
Wednesday, February 19, 2014 4:37 PM -
Hi,
This should not happen. You may have another functions which sets focus on other fields. Debug your function using the developer tools of the browser to see what happens exactly.
Wednesday, February 19, 2014 6:53 PMModerator