Change Focus to Field on Page Load

benn32
Kilo Contributor

List,
Does anyone know of a way to set focus to a field on load of a page? Currently when we 'create a new' Incident, initial focus is on the Case ID field... We'd like it to set focus to the 'username' field instead.
Any ideas would be greatly appreciated.
Thanks!

7 REPLIES 7

CapaJC
ServiceNow Employee
ServiceNow Employee

Probably something could be done with Client Scripts, but I haven't done it. Hopefully someone else can chime in.

I can tell you that default behavior is that the first writable field on the form gets focus, so that's one workaround (to move that field to be the first writable one on the form).


Mark Stanger
Giga Sage

Try something like this. It sets the focus to the 'Short Description' field on the Incident form when the form loads. Just plug it into an 'On Load' client script.


function onLoad() {
setTimeout("var refocus = document.getElementById('incident.short_description');refocus.focus();",0);
}


Thanks Mark. That works fine for setting focus to regular fields (short description) however when I try to set focus to a reference field (caller_id) it's not working. Any ideas?
THanks!


You can use a dom inspector to get the exact element ID. Generally for reference fields, the element ID is prefaced by 'sys_display'. For example, to set the focus to the caller_id field on the incident form you would use the following.


setTimeout("var refocus = document.getElementById('sys_display.incident.caller_id');refocus.focus();",0);