Javascript element iteration

October 8, 2007 13:10 by Dominick

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.

  1. var elem = document.getElementById('aspnetForm').elements;
  2. for(var i = 0; i < elem.length; i++)
  3.   {
  4.     if (elem[i].type == "submit")
  5.     {
  6.        elem[i].style.display = "none";
  7.     }
  8.   }


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 = "";


kick it on DotNetKicks.com



Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading