Since I've been doing alot of JavaScript programming lately, I figured I could start blogging some code snippets. Here is a JavaScript snippet that shows how to loop through all the elements in a form and retrieve their element type, name and values. I had to use this code to gather all form values so I could post them to the server using AJAX.
<html> <head> <script type="text/javascript"> function DisplayFormValues() { var str = ''; var elem = document.getElementById('frmMain').elements; for(var i = 0; i < elem.length; i++) { str += "<b>Type:</b>" + elem[i].type + "  "; str += "<b>Name:</b>" + elem[i].name + " "; str += "<b>Value:</b><i>" + elem[i].value + "</i> "; str += "<BR>"; } document.getElementById('lblValues').innerHTML = str; } </script> </head> <body> <form id="frmMain" name="frmMain"> <input type="hidden" name="ElemHidden" value="some hidden text" /> <input type="text" name="ElemText" value="some text" /><br /> <textarea name="ElemTextArea">Some text area text</textarea><br /> <br /> <input type="button" value="Test" onclick="DisplayFormValues();" /> </form> <hr /> <div id="lblValues"></div> </body> </html>
75287b5b-1d1f-47d1-a9ed-6d5c0eb4a545|47|3.8
JavaScript
javascript