ldsmike88 Posted March 8, 2007 Share Posted March 8, 2007 Is there a way javascript can find all of the names of the input fields in a form? Thanks! Michael Quote Link to comment Share on other sites More sharing options...
fenway Posted March 8, 2007 Share Posted March 8, 2007 Easier way it to iterate though all of the .elements of the form object... that will give you all of the INPUTs. Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 8, 2007 Author Share Posted March 8, 2007 What do you mean? Or how would I go about doing that? Quote Link to comment Share on other sites More sharing options...
fenway Posted March 8, 2007 Share Posted March 8, 2007 for( var eInput in document.forms['theForm'].elements ) { alert( eInput.name + ' - ' + eInput.type ); } Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 8, 2007 Author Share Posted March 8, 2007 When I do that it just gives me an alert box that says undefined - undefined. I can't make the alert box go away so every time I run it I have to close every IE window. It sounds like a good script, but how do I make it work right? Thanks! Michael Quote Link to comment Share on other sites More sharing options...
tomfmason Posted March 8, 2007 Share Posted March 8, 2007 Here is a very nice js form article. http://www.quirksmode.org/js/forms.html . I bet most of your questions would be answered by reading that. Tom Quote Link to comment Share on other sites More sharing options...
fenway Posted March 8, 2007 Share Posted March 8, 2007 When I do that it just gives me an alert box that says undefined - undefined. I can't make the alert box go away so every time I run it I have to close every IE window. It sounds like a good script, but how do I make it work right? Thanks! Michael Sorry, my bad, I missed a line there. Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 9, 2007 Author Share Posted March 9, 2007 Well I tried that website but I couldn't find what I wanted. I created this sweet code with the help of another website. For some reason it wont get the textarea tag names unless I run it by itself. Why doesn't it work if I just include it in the array? function getAllFormElements(formName){ var names = new Array(); var formTags = new Array('input','textarea','select','button'); for(var i = 0; i < formTags.length; i++){ formInputs = document.forms[formName].getElementsByTagName(formTags[i]); for(var i = 0; i < formInputs.length; i++){ names.push(formInputs.item(i).name); } } formInputs = document.forms[formName].getElementsByTagName(formTags[1]); for(var i = 0; i < formInputs.length; i++){ names.push(formInputs.item(i).name); } for(var i = 0; i < names.length; i++){ alert(names[i]); } } Quote Link to comment Share on other sites More sharing options...
fenway Posted March 9, 2007 Share Posted March 9, 2007 *shudder* Why not go the simpler route (sorry, I though elements was a hash only, not a collection): function getAllFormElements(formName){ var eForm = document.forms[formName]; for( var e = 0; e < eForm.elements.length; e++ ) { var el = eForm.elements[e]; alert( el.name ); } } Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 9, 2007 Author Share Posted March 9, 2007 SWEET! My code was pretty dang simple compared to the one I found, but yours is tiny and it works better! Thanks everyone! (My site is going to be so cool when I'm done!) Michael Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.