Show/Hide Form Section

rsanon
Tera Contributor

Does anyone know how to Hide/Show a form section based on a field. I attempted to follow the example below from the wiki, but it did not work for me. See attached.

function onChange(control, oldValue, newValue, isLoading) {
//this example was run on a form divided into sections (Change form)
// and hid a section when the "state" field was changed
var sections = g_form.getSections();
if (newValue == '2') {
sections[1].style.display = 'none';
} else {
sections[1].style.display = 'block';
}
}
http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#Form_Information_Methods

15 REPLIES 15

Jacob_Andersen
ServiceNow Employee
ServiceNow Employee

I prefer to show/hide form sections by name rather than by position. When doing this by position, your script will break if someone reorders the tabs. Also, it is very difficult to read the code and know which tab is being hidden/shown when done by position.

This code snippet will allow you to hide by name:



var section = $$('span[tab_caption="Schedule"]')[0].select('span[id*=section.]')[0];
section.hide();

Examples for both can be found here:
http://www.servicenowguru.com/scripting/client-scripts-scripting/showhide-form-section/

Hope this helps!


Community Alums
Not applicable

Hi Jacob,



I tried to use the code mentioned in above link. That is not working. I tried to hide variables section in RITM table.Could you please check the below code and let me know how to fix that.



var section = $$('span[tab_caption_raw="Variables"]')[0].select('span[id*=section.]')[0];


  section.hide();


  //Hide the tab


  $$('.tab_caption_text').each(function(caption) {


  if(caption.innerHTML == 'Variables'){


  caption.up('.tab_header').hide();


  }


  });




Thanks,


swamy.


wamiqkpit
Kilo Explorer

Hi,
I also tried with following code

var sections = g_form.getSections();

sections[4].style.display = 'block';

sections[4].style.display = 'none';

i tried with all values of array starting from 0 to sections.length with no if condition.. Still sections are not hiding.
also with below script

var section = $$('span[tab_caption="Schedule"]')[0].select('span[id*=section.]')[0];
section.hide();

All the fields under the section is hiding but the section tab is still visible 😞


neetusingh
Giga Guru

Hi Wamiq,

I didn't get what exactly you need, Can you please elaborate it further.

Regards/Neetu


Hi Neetu,

I've created a new section say "xyz". Now i want this section to get display only on a particular state i.e. "Resolved".
I tried both ways shown in this post

1]
var sections = g_form.getSections();
if (newValue == 7) {
sections[4].style.display = 'none';
} else {
sections[4].style.display = 'block';
}

But it is not working. i check the condition is right. i tried removing condition just to see it work, but still no luck.
I thought might be the array index is not pointing correct.
so just to see i tried every index of the array as follow,

var sections = g_form.getSections();
for(i=0; i
{
sections.style.display = 'none';
}

but still didn't work.

2]
var section = $$('span[tab_caption="Schedule"]')[0].select('span[id*=section.]')[0];
section.hide();

I tried with above code also.
It hides all the fields under the section. but the title tab of the section is still visible. 😞

Please suggest.

Happy Thursday,
Wamiq