I recently needed to loop through all the elements on a form on the client-side. I wanted to be able to produce a printable form without a page refresh. This bit of code loops through all form elements on an ASP.NET form and simply hides all the buttons on the form. This will produce a much cleaner form for printing.
- var elem = document.getElementById('aspnetForm').elements;
- for(var i = 0; i < elem.length; i++)
- {
- if (elem[i].type == "submit")
- {
- elem[i].style.display = "none";
- }
- }
This provides a printer friendly form with no page refresh. It's easy enough to reverse the process by looping through the elements again using this instead:
elem[i].style.display = "";