www.webdeveloper.com
Results 1 to 7 of 7

Thread: newbie here

  1. #1
    Join Date
    Sep 2006
    Location
    outer space
    Posts
    2

    newbie here

    hello everbody!
    I am making a grocery list with DHTML (Dynamic HTML). I have the layout done, and have a button that says, "calculate total" I want so that when I pres that button it adds the numbers together, and a window.alert comes up that tells me the total.
    here is my code that I have now:


    Code:
    function calculate_total() {
    window.alert( document.getElementById("number1").value document.getElementById("number2").value + document.getElementById("number3").value + document.getElementById("number4").value + document.getElementById("number5").value + document.getElementById("number6").value + document.getElementById("number7").value + document.getElementById("number8").value + document.getElementById("number9").value + document.getElementById("number10").value + document.getElementById("number11").value = document.getElementById("number12").value); 
    }
    calculate_total is the onclick from button.

    thanks.

  2. #2
    Join Date
    Jun 2006
    Posts
    384
    So what, you going in for the longest line of code prize?

    HTML Code:
    function calculate_total() {
    
      var iTot=0;
      for (var i=1; i<12; i++)
      {
          iTot += parseInt(document.getElementById("number"+string(i)).value )
      }
      alert(iTot);
    }
    should work, not tested may have typos.

  3. #3
    Join Date
    Sep 2006
    Location
    outer space
    Posts
    2
    Hello, Cridley.
    That didn't work.
    sorry

    does anyone know why my original code won't work?

  4. #4
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Read the guidelines for posting first. Format your code so it is readable - use more lines. You are missing a '+' sign and have a '=' sign in your alert statement...it doesn't make sense. Cridley's approach is fine he just missed some...
    Code:
    function calculate_total() {
      var iTot=0;
      for (var i=1; i<13; i++)
        iTot += parseInt(document.getElementById("number"+i).value);
      alert(iTot);
    }
    Last edited by Dok; 09-26-2006 at 07:51 PM.

  5. #5
    Join Date
    Aug 2006
    Location
    Turkey
    Posts
    242
    Code:
     
    <script type="text/javascript">
    
    function total(){
    
    
    result=0 ;
    
    var input=document.getElementsByTagName('input')
    
    for(i=0;i<input.length; i++){
    
    if( input[i].type=="text")  {
    
    result+= input[i].value * 1; 
    
    }
    }
    alert(result)
    }
    </script>
    
    <form name="form">
    <input type="text" value="" id="number1"><br>
    <input type="text" value="" id="number2"><br>
    <input type="text" value="" id="number3"><br>
    <input type="text" value="" id="number4"><br>
    <input type="text" value="" id="number5"><br>
    <input type="text" value="" id="number6"><br>
    <input type="text" value="" id="number7"><br>
    <input type="text" value="" id="number8"><br>
    <input type="text" value="" id="number9"><br>
    <input type="text" value="" id="number10"><br>
    <input type="text" value="" id="number11"><br>
    <input type="text" value="" id="number12"><br>
    <input type="button" value="calculate" onclick="total()" ><br>
    </form>
    Code:
     
    <script type="text/javascript">
    
    function total(){
    
    result=0 ;
    
    for(i=1; i<13; i++){
    
    result+= document.getElementById("number"+i).value * 1 ; 
    
    }
    
    alert(result)
    }
    </script>
    
    <form name="form">
    <input type="text" value="" id="number1"><br>
    <input type="text" value="" id="number2"><br>
    <input type="text" value="" id="number3"><br>
    <input type="text" value="" id="number4"><br>
    <input type="text" value="" id="number5"><br>
    <input type="text" value="" id="number6"><br>
    <input type="text" value="" id="number7"><br>
    <input type="text" value="" id="number8"><br>
    <input type="text" value="" id="number9"><br>
    <input type="text" value="" id="number10"><br>
    <input type="text" value="" id="number11"><br>
    <input type="text" value="" id="number12"><br>
    <input type="button" value="calculate" onclick="total()" ><br>
    </form>
    Code:
     
    <script type="text/javascript">
    
    function total(){
    
    result=0 ;
    
    for(i=1; i<13; i++){
    
    result+= Number(document.getElementById("number"+i).value) ; 
    
    }
    
    alert(result)
    }
    </script>
    
    <form name="form">
    <input type="text" value="" id="number1"><br>
    <input type="text" value="" id="number2"><br>
    <input type="text" value="" id="number3"><br>
    <input type="text" value="" id="number4"><br>
    <input type="text" value="" id="number5"><br>
    <input type="text" value="" id="number6"><br>
    <input type="text" value="" id="number7"><br>
    <input type="text" value="" id="number8"><br>
    <input type="text" value="" id="number9"><br>
    <input type="text" value="" id="number10"><br>
    <input type="text" value="" id="number11"><br>
    <input type="text" value="" id="number12"><br>
    <input type="button" value="calculate" onclick="total()" ><br>
    </form>
    Code:
     
    <script type="text/javascript">
    
    function total(){
    
    
    result=0 ;
    
    var input=document.getElementsByTagName('input')
    
    for(i=0;i<input.length; i++){
    
    if( input[i].type=="text")  {
    
    result+= Number(input[i].value); 
    
    }
    }
    alert(result)
    }
    </script>
    
    <form name="form">
    <input type="text" value="" id="number1"><br>
    <input type="text" value="" id="number2"><br>
    <input type="text" value="" id="number3"><br>
    <input type="text" value="" id="number4"><br>
    <input type="text" value="" id="number5"><br>
    <input type="text" value="" id="number6"><br>
    <input type="text" value="" id="number7"><br>
    <input type="text" value="" id="number8"><br>
    <input type="text" value="" id="number9"><br>
    <input type="text" value="" id="number10"><br>
    <input type="text" value="" id="number11"><br>
    <input type="text" value="" id="number12"><br>
    <input type="button" value="calculate" onclick="total()" ><br>
    Last edited by samanyolu; 09-27-2006 at 05:57 AM.

  6. #6
    Join Date
    Jun 2006
    Posts
    384
    Quote Originally Posted by Dok
    Cridley's approach is fine he just missed some...
    Thanks for correcting my code, it was not tested, mainly just illustrating how to go about it.

    samanyolu's approach is also good but it assumes you have no other text fields on your form (which may or may not be the case)
    Last edited by cridley; 09-27-2006 at 04:49 AM.

  7. #7
    Join Date
    Sep 2006
    Location
    CA
    Posts
    1

    Smile Introduce myself

    I would like to introduce myself. I am sure this is not the place, but I have been know to change the rules. I have a website but I need a couple things changed. But I am not sure if it is "do-able"; or legal.
    Anybody curious????

Thread Information

Users Browsing this Thread

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

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