How to set the focus i.e.,cursor to a field

Community Alums
Not applicable

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.

5 REPLIES 5

Arun_Manoj
Mega Sage

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

Arun_Manoj
Mega Sage

Hi @Community Alums ,

please check Example

arunm1_0-1747819454097.pngarunm1_1-1747819488085.png

 

Community Alums
Not applicable

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

Ankur Bawiskar
Tera Patron
Tera Patron

@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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader