Loop through all elements in a form : Elements « Form « JavaScript Tutorial

JavaScript Tutorial
1. Language Basics
2. Operators
3. Statement
4. Development
5. Number Data Type
6. String
7. Function
8. Global
9. Math
10. Form
11. Array
12. Date
13. Dialogs
14. Document
15. Event
16. Location
17. Navigator
18. Screen
19. Window
20. History
21. HTML Tags
22. Style
23. DOM Node
24. Drag Drop
25. Object Oriented
26. Regular Expressions
27. XML
28. GUI Components
29. Dojo toolkit
30. jQuery
31. Animation
32. MS JScript
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript Tutorial » Form » Elements 
10. 4. 4. Loop through all elements in a form
<html>
<head>
<title>Online Survey</title>
<script type="text/javascript" language="javascript">
<!-- //
function CheckCheckboxes(){
    var elLength = document.MyForm.elements.length;

    for (i=0; i<elLength; i++)
    {
        var type = MyForm.elements[i].type;
        if (type=="checkbox" && MyForm.elements[i].checked){
            alert("Form element in position " + i + " is of type checkbox and is checked.");
        }
        else if (type=="checkbox") {
            alert("Form element in position " + i + " is of type checkbox and is not checked.");
        }
        else {
        }
    }
}
// -->
</script>
</head>
<body>
<form action="http://www.java2java.com" method="POST" name="MyForm">
<table width="600">
<tr><th colspan="3" align="center">Online Survey<br /><br /></th>
</tr>
<tr>
 <td>Your Name:</td>
 <td>&nbsp;</td>
 <td><input type="text" name="YourName"/></td></tr>
<tr>
 <td>Your Gender:</td>
 <td>&nbsp;</td>
 <td>
   <input type="radio" name="Gender" value="Male"/>Male<br />
   <input type="radio" name="Gender" value="Female"/>Female<br />
 </td>
</tr>
<tr><td>Which of our consultancy <br />services are you interested in?</td>
 <td align="right">&nbsp;
 </td>
 <td>
   <input type="checkbox" name="XML"/>&nbsp;XML<br />
   <input type="checkbox" name="XSLT"/>&nbsp;XSLT<br />
   <input type="checkbox" name="SVG"/>&nbsp;SVG<br />
   <input type="checkbox" name="XSL-FO"/>&nbsp;XSL-FO<br />
   <input type="checkbox" name="XForms"/>&nbsp;XForms<br />
 </td>
</tr>
<tr>
 <td>Which free gift would you prefer for filling out this survey?</td>
 <td>&nbsp;</td>
 <td>
  <select name="FreeGift">
   <option value="Choice1">Choice 1</option>
   <option value="Choice2">Choice 2</option>
   <option value="Choice3">Choice 3</option>
  </select>
</tr>
<tr>
 <td>Enter your comments in<br />the text box
 </td>
 <td>&nbsp;</td>
 <td><textarea name="Comments" rows="5" cols="50"></textarea></td>
</tr>
<tr>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td><br /><input type="submit" value="Send Form" onclick="CheckCheckboxes()"/></td>
</tr>
</table>
</form>
</body>
</html>
10. 4. Elements
10. 4. 1. Accessing and Writing Information
10. 4. 2. Writing information to forms.
10. 4. 3. Reference form element by using the dot notation
10. 4. 4. Loop through all elements in a form
10. 4. 5. Check Form element type
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.