Current location: Hot Scripts Forums » General Web Coding » JavaScript » For Loop and innerText


For Loop and innerText

Reply
  #1 (permalink)  
Old 09-17-09, 04:09 AM
KaiserClaw KaiserClaw is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
For Loop and innerText

Hi,

I have some problem with my loop and innerText.

I have three input-fields. When you write a number I want the innerText-function to write the number, and when you add another number I want it to add those.
It suppose to display the number added and when another is added it will add them and so on.

Example:

If I write "3" in answer1 the innerText is supposed to display: 3
If I write "3" in answer1 and "5" in answer2 the innerText is supposed to displays: 8
and so on.....

The problem is that when I add a number in answer1 it just says "NaN" but when I fill answer1, answer2 and answer3 it says the correct sum.

My code:
Code:
function onLoad(){

for(i=0; i<3; i++){
document.getElementById("answer"+[i]).onkeyup=function(){ count();
}
}
function count(){

for(i=0; i<3; i++){

sum+=parseFloat(document.getElementById("answer"+[i]).value); 

document.getElementById("SUM").innerText =+sum;
}

}
It's very important to keep the loops! I can't use another script without the loops!

Thanks'

Last edited by wirehopper; 09-17-09 at 06:59 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-17-09, 06:24 AM
KaiserClaw KaiserClaw is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I tried this function, but it doesn't work 100%. It displays all numbers, but it wont add them together...
Code:
function count()
{
  document.getElementById("SUM").innerText = "";

  for (i=0; i<=4; i++) {
  	var bo;
  	bo=(document.getElementById("answer"+[i]).value);
    document.getElementById("MESS").innerText +=(bo);
  }
}

Last edited by wirehopper; 09-17-09 at 06:59 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-17-09, 06:58 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Code:
function count()
{
  document.getElementById("SUM").innerText = "";
total=0;
  for (i=0; i<=4; i++) {
  	var bo;
  	bo=(document.getElementById("answer"+[i]).value);
                     total+=bo;
    }
    document.getElementById("MESS").innerText =total;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-17-09, 09:32 AM
KaiserClaw KaiserClaw is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Prints: 0123 when I put "1" in answer1, "2" in answer2 and "3" in answer3

Suppose to print: "6" by adding 1+2+3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-17-09, 10:20 AM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Took WireHopper's code and added parseInt method.

Code:
function count()
{
  document.getElementById("SUM").innerText = "";
total=0;
  for (i=0; i<=4; i++) {
  	var bo;
  	bo=(document.getElementById("answer"+[i]).value);
                     total+=parseInt(bo);
    }
    document.getElementById("MESS").innerText =total;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 09-17-09, 11:18 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by carters-site View Post
Took WireHopper's code and added parseInt method.
Yep, Javascript is the only language I know where "2 + 2" equals "22".

C, C++, perl, PHP, java, Forth, BASIC, COBOL, LISP, etc etc....all those will give you "4". But not Javascript, noooooo. That'd be too easy.

I don't know what they were smoking when they decided on string concatenation as the default behavior for "+", but it must have been some really, really strong stuff.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]

Last edited by End User; 09-17-09 at 11:20 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 09-17-09, 12:04 PM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Quote:
Originally Posted by End User View Post
Yep, Javascript is the only language I know where "2 + 2" equals "22".

C, C++, perl, PHP, java, Forth, BASIC, COBOL, LISP, etc etc....all those will give you "4". But not Javascript, noooooo. That'd be too easy.

I don't know what they were smoking when they decided on string concatenation as the default behavior for "+", but it must have been some really, really strong stuff.
LOL! Yes one of the many annoyances of javascript. The other is debugging in IE... / wrist
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 10-13-09, 08:27 AM
hscriptnew hscriptnew is offline
New Member
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Hai I have to use an for loop inner text some problem occur repeated to check an one statement executed
for (i=0; i<=4; i++) {
var bo;
bo=(document.getElementById("answer"+[i]).value);
total+=parseInt(bo);

}

Last edited by wirehopper; 10-13-09 at 08:55 AM. Reason: Removed advertising
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 10-16-09, 12:31 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I am not exactly sure how you have your HTML setup,
so I setup a form with 5 input elements.
1 - has id="answer0"
2 - has id="answer1"
3 - has id="answer2"
4 - has id="SUM"
5 - is a submit button

The first thing that happens is the window.onload event,
which runs a function that attaches the onkeyup event listener to all input elements that have the word "answer" in their id,
and assigns the function count() to the event listener.

The function count() gets the values from all three input elements and adds them together,
then places the results into the input element with id "SUM".
HTML Code:
<html>
<head>
<script>
window.onload = function()
{
 var objs = document.getElementsByTagName("input");
 for(var i=0;i<objs.length;i++)
 {
  if(objs[i].id.substr(0,objs[i].id.length-1) == "answer")
  {
   objs[i].onkeyup=count;
   }
  }
 }
function count()
{
 var objs = document.getElementsByTagName("input");
 var total = 0;
 for(var i=0;i<objs.length;i++)
 {
  if(objs[i].id.substr(0,objs[i].id.length-1) == "answer")
  {
   total += objs[i].value ? parseInt(objs[i].value) : 0;
   }
  }
 document.getElementById("SUM").value = total;
 }
</script>
</head>
<body>
<form action="processForm.php" method="post">
 <table>
  <tr>
   <td>Value 1: </td>
   <td><input type="text" id="answer0" /></td>
  </tr>
  <tr>
   <td>Value 2: </td>
   <td><input type="text" id="answer1" /></td>
  </tr>
  <tr>
   <td>Value 3: </td>
   <td><input type="text" id="answer2" /></td>
  </tr>
  <tr>
   <td align="right">Total: </td>
   <td><input type="text" id="SUM" /></td>
  </tr>
  <tr>
   <td colspan="2" align="right"><input type="submit" name="submit" value="Submit" /></td>
  </tr>
 </table>
</form>
</body>
<html>
You shouldn't have any problems modifying this code to fit your HTML.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 10:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.