The CreatorCon Call for Content is officially open! Get started here.

How do I change the form or tab focus after a field changes?

russellj03
Giga Contributor

The system I work on displays a closure section when incidents are marked as closed, which is done with a drop-down menu vs. a UI action.   The section is off the screen, however, and it is confusing because users don't know it is there until they hit update and get an error popup.   I am trying to change the focus with a UI Policy or onChange Client Script, but nothing has worked so far.

In my Client Script, I know I have the field change detecting correctly because I have an alert popping up.   I have tried using the g_tabs2Sections API:

1) by putting in the section numbers directly (.setActive(2))

2) by looking up the section ID in the source and finding the index of that sysID (.findTabIndexByID(sysID)), which was returning 1.

Neither section number is setting the focus.   What am I doing wrong?

Here is the main part of the script:

var id = g_tabs2Sections.findTabIndexByID('section_tab.12345678...');

alert('id = ' + id);

g_tabs2Sections.setActive(id);

1 ACCEPTED SOLUTION

Hi Justin,



I don't know what's causing the issue, but you could try wrapping that part in a timeout:


setTimeout(function() {g_form.getElement('incident.close_notes').focus();},0);



That way it executes after the client script finishes- perhaps the element has to hold focus for the duration of the script execution. That sounds plausible.


View solution in original post

17 REPLIES 17

Cory,



You are the man!   I had tried the setTimeout function but used slightly different syntax, which was unsuccessful.   This worked perfectly.



Thanks!


Justin


I agree with Cory.



Justin,



What do you get if you simple alert g_form.getElement('incident.close_notes')? Also is this script working on load or change of the page. Since the tab is hidden the field could be hidden so the it might require to first make the tab visible before setting focus on the field.


Venkat,



Yes, that is what I ended up with.   It is an onChange client script that sets the active tab and then refocuses to a field on that tab.   Everything is working great now.



Thanks,


Justin


Would you mind sharing your finished script Justin as I have a very similar requirement



Many thanks


Angus - Sure, but I have to re-type since our system is not connected to the internet.   If you try it and run into a problem, let me know as I may have entered a typo.   Also, if you need help with the section sys_id, go to System UI -> Form Sections and search for the form section on the table you need.



Thanks,


Justin




function onChange(control, oldValue, newValue, isLoading, isTemplate) {


        if (isLoading || newValue == '') {


                  return;


        }



        //Change to Closure tab when incident state is closed


        if (newValue == 7 || newValue == 8 || newValue == 12) {


                  var id = g_tabs2Sections.findTabIndexByID('section_tab.[your section sys_id here]');         //"Closure Information"


                  g_tabs2Sections.setActive(id);


                  setTimeout(function() {g_form.getElement('incident.close_notes').focus();},0);



                  document.getElementId("label.incident.close_notes").hint = "Description of the actions taken to investigate and resolve this incident.";


        }


}