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

krishnakoneru
Kilo Contributor

Hi Rachelle,

I can't find anything wrong with the script. may be you are not using the right section number inside [].
So try with diffrent numbers like sections[2].style.display or sections[3].style.display etc.

Thank you


Thanks! But I did check the form section number. Very Odd, do you know of another solution?


neetusingh
Giga Guru

Hi Rachelle,

I just tried it on https://demo019.service-now.com/navpage.do. Its working good. Please check it on incident form, where I wrote the below on change script on field - Contact-type. Based on the value selected, section will get display. For Eg: If we choose Email , section-email will appear.

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 == 'email') {
sections[4].style.display = 'block';
} else {
sections[4].style.display = 'none';
}

if (newValue == 'phone') {
sections[5].style.display = 'block';
} else {
sections[5].style.display = 'none';
}

if (newValue == 'self-service') {
sections[6].style.display = 'block';
} else {
sections[6].style.display = 'none';
}

}

I hope this would provide you some direction.

Note : Please check if you are comparing the database value, not the Label Name in the IF Statement


Thank you Neetu!