Monday, June 18, 2012

JavaScript - how to reset a form, including dropdown lists (list boxes)

You have a really big form you want to clear. It contains with text boxes, text areas, dropdown lists (list boxes), check boxes, radio buttons. One way to do it is to fire the Javascript forms.reset function but it will not take care of the dropdown lists (list boxes). That's the way I did it (assuming you have all of your list boxes have similar prefix ID, like I do - lstbx)



function form_reset(){
     
      document.forms[0].reset();
     
      var element_names = new Array("01","02","03", "04", "05", "06");
     
      for(var i=0; i <=element_names.length; i++)
            {
                  clear_my_list_box(document.getElementById('lstbx_' + element_names[i]));
            }
}
function clear_my_list_box(lstbx) {

      var i;
      for (i = lstbx.options.length - 1; i >= 0; i--) {
            lstbx.remove(i);
      }

}



Cheers!

No comments:

Post a Comment