SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how can i count form inputs with javascript?

    Hi Guys,

    How can I count form array inputs like "input_name[]" using javascript?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    formname.getElementsByTagName('input').length

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will that actually count the number of inputs or the length of the input?

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's how many <input> tags are inside of the form.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    formname.getElementsByTagName('input') is an array containing all of the input fields in the form. When you get the length of an array it tells you how many entries are in the array.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     <script type="text/javascript">
    window.onload= function count () {
    var e = document.forms["form1"] ;
    e= e.getElementsByTagName('input');
    alert(e.length) ; // 6
    var n = document.forms["form1"].elements["aa"].length;
    alert(n) ;  // 4
    }
    </script>
    <body>
    <form name="form1">
    <input type="text" name="aa"><br>
    <input type="radio" name="aa"><br>
    <input type="checkbox" name="bb"><br>
    <input type="password" name="aa"><br>
    <input type="image" name="bb"><br>
    <input type="submit" name="aa"><br>
    </form>

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anyway to count only one specific input field?

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean with a specific input field? Of type text etc?

  9. #9
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No I mean count all occurences of a specific field name. For example, I have a form that has several inputs with the same name:

    Code:
    <input name="venue_name[]" type="text">
    <input name="venue_name[]" type="text">
    <input name="venue_name[]" type="text">
    How can I count all occurences of "venue_name"?

  10. #10
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <body>
    
    <input name="venue_name[]" type="text">
    <input name="venue_name[]" type="text">
    <input name="venue_name[]" type="text">
    
    <script type="text/javascript">
    
    var e = document.getElementsByTagName('input');
    
    var i ; 
    var s = 0 ;
    for(i=0; i < e.length; i++) {
    
    if(e[i].type== "text" && e[i].name=="venue_name[]" ) { s++  ; }
    }
     alert ( s ) ;
    
    </script>

  11. #11
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that worked a treat!

  12. #12
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no need to use getElementsByTagName in this case, use getElementsByName instead:
    Code:
    <input name="venue_name[]" type="text">
    <input name="venue_name[]" type="text">
    <input name="venue_name[]" type="text">
    
    
    <script>
    var venueInputs = document.getElementsByName("venue_name[]");
    
    alert(venueInputs.length);
    </script>


Bookmarks

Posting Permissions

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