How to set the focus i.e.,cursor to a field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 02:05 AM
Hi all,
I have written an onchange where we an certain condition meet it need to show an alert and then cursor has to move to a certain field. I have used g_form.getControl('field_name').focus(); and also g_form.getControl('field_name').scrollIntoView() but both the things are not working i have make the isolate script as false as well true even then it doesnt work. Can anyone help me how to resolve these issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 02:23 AM
Hi @Community Alums ,
Please try the below code
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
if (newValue === 'some_condition') {
alert("This field is required.");
setTimeout(function() {
var targetControl = g_form.getControl('target_field');
if (targetControl) {
targetControl.focus();
targetControl.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}, 300);
}
}
Thanks
Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 02:24 AM
Hi @Community Alums ,
please check Example
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 03:24 AM
Hi @Arun_Manoj
Thanks for the reply. This was working fine in native ui hr case but when i open a case in agent workspace it was not working. Can you please help me how to fix it in agent workspace as well
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 02:37 AM
@Community Alums
it should work fine, it doesn't have any dependency with Isolate Script field
share your complete script and screenshots.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader