What is use of focus() function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 08:24 PM
Hello,
Can anyone please explain what is the use of focus()? and also can you please look into the below code
Code :
function onLoad() {
var a = $('IO:d0cc39e2d7532100a9bd1e173e24d489');
if (a)
a.focus();
}
Actually, this was created long back but I did not understand what is the use of this code. if it is not required, Can I deactivate this script?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 08:40 PM
Hi
Documentation for the .focus() method states it "is used to give focus to an element (if it can be focused)". This means the method can only be used on HTML elements, and not all elements support the function: https://www.w3schools.com/jsref/met_html_focus.asp
A common request from ServiceNow users is how to set the focus on a field. If you don't know what this is, bringing focus to a field refers to making said field active which will normally cause a web page to scroll the underlying HTML element into view for the user.
This action is desired in many situations, though it is mainly requested when a user attempts to submit a form with missing information. The field is set to mandatory and the focus is set to the field which the user will see and finish filling out the form.
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);
Please mark the Appropriate answers correct & Helpful
Thanks,
Sandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 08:46 PM
Hi,
Script is checking for a particular DOM element ID for a field or an element on your form and is trying to highlight it when ever the form loads.
Using DOM is not at all a good practice as that ID may change during an ServiceNow upgrade, so would suggest if this is not needed you can disable it as it may affect performance as well.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 08:48 PM
Hi,
If the intent is to use focus then a better approach here would be instead of using DOM use the steps below:
Open the catalog item / record producer in question
2) Go to the related list "Catalog Client Scripts" and click New
3) Fill out the fields as necessary and make sure that Type is onLoad.
4) On the script field paste the following code:
setTimeout(function(){
g_form.getElement('name_of_variable').focus();
}, 0);
*Replace 'name_of_variable' parameter with the actual name of the variable
Adding the HI article as well to back my answer on this:
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0755305
To learn more on Focus please visit the W3 school link below:
https://www.w3schools.com/jsref/met_html_focus.asp
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 09:30 PM
Hello.
Thanks for the quick response.
What is the meaning of this code"$('IO:d0cc39e2d7532100a9bd1e173e24d489');"?