www.webdeveloper.com
Results 1 to 3 of 3

Thread: All formfields in javascript?

Hybrid View

  1. #1
    Join Date
    Jul 2009
    Posts
    1

    Question All formfields in javascript?

    Hello everyone.

    First I want to say: I'm from the Netherlands, and my Engelish is not that good, so sorry for that

    My problem:

    I have a form. With a database I get some variables and with that variables I make some input fields. But, the number of fields can be different, and they have different names.

    I want to submit the form with ajax, but before I can do that, I have to know what input fields I have. So, my question is: how can I figure out which input fields I have, and, how can I put them as arguments in a js-function?

    I hope you people can help me...

    Greetings,
    Igor0208

  2. #2
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    945
    Is it just one form? If so, loop through the childNodes of document.forms[0].

    Code:
    <script>
    function countFields() {
    	var fieldsCounter = 0;
    	for (var k = 0; k < document.forms[0].childNodes.length; k++) {
    		var tag;
    		if (typeof (tag = document.forms[0].childNodes[k]).tagName != "undefined") {
    			if (tag.tagName.toLowerCase() == "input" && tag.type.toLowerCase() != "submit") fieldsCounter++;
    		}
    	}
    	alert(fieldsCounter);
    }
    </script>
    
    <form action="javascript:countFields();">
    	<input type="text" id="field1" />
    	<input type="text" id="field1" />
    	<input type="submit" value="asd" />
    </form>

  3. #3
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    why did you use childNodes?

    You can use elements
    Code:
    document.forms[0].elements.length
    document.forms[0].elements[0].value
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles