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

Hi Justin,



If youa re in Chrome, and you accessed the form normally, then you have to account for the fact that the form is in an iframe. You can do one of two things:



Navigate directly to the form by going to:


      https://your-instance-url/incident_list.do


Open the individual incident.



Alternatively, in the Console, make sure you have chosen the correct frame to run the code in:


Screen Shot 2015-10-08 at 10.31.55 AM.PNG


Cory,



Thanks for that.   Picking the right frame makes a big difference.  



I realize now that I have two issues and I was incorrectly expecting the active-tab thing to address both.   The setActive is working now, but I also want to simultaneously change the focus of the form to one of the fields (close_notes) on that tab.



I added this line to the client script:


        g_form.getElement('incident.close_notes').focus();



It doesn't work in the client script, but it does when I run it from the console.   Any ideas on how to make the client script work?



Thanks,


Justin


Hi Justin,



Do you get an errors or logs int he console, when running that from the client script versus doing it from the console? They are both just telling Javascript to access the g_form object and call the getElement method on it, then setting focus on that element. The only reason it wouldn't work in a client script is if it's a Scoped client script, in which case access to getElement is not available and you'd see an error message.


Cory,



No errors.   I agree there shouldn't be a difference between the two, but it seems like there is.



Thanks,


Justin


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.