Onload Client Script

vipultiwar01
Kilo Contributor

function onLoad() {

  var sections = g_form.getSections();

if (category== 'SERVER') {

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

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

} else {

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

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

}

}

Please help in the correction of this code. I would like to display the section based on"category" in maintain item form but it is not working. Once I removed the "if else" condition then it is working .

5 REPLIES 5

Kannan Nadar
Tera Guru

Hi Vipul,



I don't see the variable "category" defined anywhere, first define category and then use the below code to hide or display sections.



var sections = g_form.getSections();


    if (newValue == '2') {


    g_form.setSectionDisplay(sections[1], false);


    } else {


    g_form.setSectionDisplay(sections[1], true);


    }


divya mishra
Kilo Sage

Hey Vipul,




Could you please try :




function onLoad() {


  var sections = g_form.getSections();


if (g_form.getValue('category') == 'SERVER') {


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


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


} else {


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


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


}





Let me know if this was helpful/correct


Have a wonderful day ahead






Regards,


Divya Mishra


Surendra6
Tera Contributor

Hi ,

Client Script is one of the important part of the Servicenow. 

Aman Kumar S
Kilo Patron

I think there are two things that you need to do here:

1. Onload client script, to hide section when the form loads based on category.

2. OnChange client script, to hide section, when somebody changes the category on the form.