adding values of text box in loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaya s
    New Member
    • Dec 2011
    • 1

    adding values of text box in loop

    hi,
    i am new to js and html.
    i need a help to solve this prob..
    i need the output as the values entered in form to alert.by using string with loop.i tried following code...can anyone help in this

    Code:
    <html>
    <head>
    <title>elements in a form</title>
    <script type="text/javascript">
    function processFormData() 
    {
    var len= document.getElementsByTagName('name');
    
    var str1=null;
    for(i=0;i<=len;i++)
    {
    var str=(subscribe.name[i].value);
    
    str=str1+str; 
     }
      alert(str);
      
    }
    
    </script>
    </head>
    <body>
    
    <form name ="subscribe" > 
    	<p><label for="name">Your Name: </label><input type="text" name="name" id="txt_name" value="name"></p>
    	<p><label for="email">Your Email: </label><input type="text" name="email" id="txt_email" value="mail"></p>
    <input type="button" value="submit" onclick="processFormData()">
    	
    
    </form>
    </body>
    </html>
    Last edited by Meetee; Dec 9 '11, 09:20 AM. Reason: code tags added
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    There is no HTML NAME tag. You probably meant to use the input tag.

    Comment

    • omerbutt
      Contributor
      • Nov 2006
      • 638

      #3
      hi
      two things in this line
      Code:
      var len= document.getElementsByTagName('name');
      1. name is none of the HTML tags as Rabbit said .
      2. this statement wont get the length or the number of inputs that you are trying to loop to, it will contain the object of HTMLcollection of input or any other valid tag that is given here.

      so if we correct it , the it goes like
      Code:
      var len= document.getElementsByTagName('input').length;
      after that where does the subscribe come from , the form is not accesed like this and you do not need to access the form when you have the collection of HTML inputs.here look at the following code
      Code:
          <html>
          <head>
          <title>elements in a form</title>
          <script type="text/javascript">
          function processFormData() {
      		var elem_collection= document.getElementsByTagName('input');
      		var total_elem=elem_collection.length;
          	var str1='';
      		
      		for(i=0;i<total_elem-1;i++){
      			str1+=elem_collection[i].value;	
      		}
      		alert(str1);
          }
           
          </script>
          </head>
          <body>
           
          <form name ="subscribe" > 
              <p><label for="name">Your Name: </label><input type="text" name="name" id="txt_name" value="name"></p>
              <p><label for="email">Your Email: </label><input type="text" name="email" id="txt_email" value="mail"></p>
          <input type="button" value="submit" onclick="processFormData()">
           
           
          </form>
          </body>
          </html>
      regards,
      Omer Aslam

      Comment

      Working...