473,409 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

adding values of text box in loop

1
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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>elements in a form</title>
  4. <script type="text/javascript">
  5. function processFormData() 
  6. {
  7. var len= document.getElementsByTagName('name');
  8.  
  9. var str1=null;
  10. for(i=0;i<=len;i++)
  11. {
  12. var str=(subscribe.name[i].value);
  13.  
  14. str=str1+str; 
  15.  }
  16.   alert(str);
  17.  
  18. }
  19.  
  20. </script>
  21. </head>
  22. <body>
  23.  
  24. <form name ="subscribe" > 
  25.     <p><label for="name">Your Name: </label><input type="text" name="name" id="txt_name" value="name"></p>
  26.     <p><label for="email">Your Email: </label><input type="text" name="email" id="txt_email" value="mail"></p>
  27. <input type="button" value="submit" onclick="processFormData()">
  28.  
  29.  
  30. </form>
  31. </body>
  32. </html>
Dec 9 '11 #1
2 3618
Rabbit
12,516 Expert Mod 8TB
There is no HTML NAME tag. You probably meant to use the input tag.
Dec 9 '11 #2
omerbutt
638 512MB
hi
two things in this line
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1.     <html>
  2.     <head>
  3.     <title>elements in a form</title>
  4.     <script type="text/javascript">
  5.     function processFormData() {
  6.         var elem_collection= document.getElementsByTagName('input');
  7.         var total_elem=elem_collection.length;
  8.         var str1='';
  9.  
  10.         for(i=0;i<total_elem-1;i++){
  11.             str1+=elem_collection[i].value;    
  12.         }
  13.         alert(str1);
  14.     }
  15.  
  16.     </script>
  17.     </head>
  18.     <body>
  19.  
  20.     <form name ="subscribe" > 
  21.         <p><label for="name">Your Name: </label><input type="text" name="name" id="txt_name" value="name"></p>
  22.         <p><label for="email">Your Email: </label><input type="text" name="email" id="txt_email" value="mail"></p>
  23.     <input type="button" value="submit" onclick="processFormData()">
  24.  
  25.  
  26.     </form>
  27.     </body>
  28.     </html>
  29.  
  30.  
  31.  
regards,
Omer Aslam
Dec 10 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: sean | last post by:
HI There, I am trying to total the value of some form fields I am referecing the form field with an inner loop, I am having a little trouble because the amount of fields returned is always...
3
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound...
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
4
by: David Plotts | last post by:
I'm a beginner with VB.net, only had one class in college on it. I can't seem to remember how to add text boxes up. I want to add the values in text boxes together, and put the value into a...
2
by: Srinivas3279 | last post by:
I am new to the C/C++ My Program: int main(int argc, _TCHAR* argv) { //Declarations FILE *fp;
15
by: kigoobe | last post by:
Well friends, I am having a table like this ... http://kigoobe.com/offshoring/bugs/javascript/javascriptAddData.gif Where, the left most and right most columns have zero values when the page...
5
by: Zachary | last post by:
So I have a text box with a string in it, and I want to get the value in that text box into a table using VBA but I have no idea how to do that. Anyone offer any help? Thanks in advance!
8
by: crayfiss | last post by:
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up. I have a set of radio buttons with values to it and a select...
4
by: ranjini ns | last post by:
Hi, I have a form, and in this form, there are 2 drop down list a text box, one button (labelled "add" )and finally, a listbox. The question is, when the user clicks on the add button, how do I...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.