Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question window.alert not working. Please help!

    I am trying to get the total of the checkboxes selected when the user clicks the "calculate total" button. It isn't working though. What am I doing wrong?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function calculate(f)
    {
    var nums = f.num;
    var ntext = f.numtext;
    var nitem = f.numitem;
    var result = 0;
    var items = '';
    for(var i=0;i<nums.length;i++)
    {
    if(nums[i].checked)
    {
    result+=parseFloat(ntext[i].value);
    items+=nitem[i].value+'\n';
    }
    }
    f.answer.value=result;
    }
    if (nums[i].checked) {
    window.alert("Your total is " + f.answer.value=result;);
    
    }
    </script>
    
    </head>
    <body>
    <style type="text/css">
    .box1 { float:left; 
    		width:500px;
    		clear:left;
    		}
    .box2 { float:left;
    padding-left:50px;
    }
    </style>
    <form name="myform">
    Build your own family meal<br>
    <div class="box1">
    <input type="checkbox" name="num" onclick="calculate(this.form)">
    <input type="hidden" name="numtext" value="24.00" onchange="calculate(this.form)">
    <input type="hidden" name="numitem" value="pencil" onchange="calculate(this.form)">
    <div class="box2">Chicken Enchiladas $24.00</div>
    </div><br>
    
    <div class="box1">
    <input type="checkbox" name="num" onclick="calculate(this.form)">
    <input type="hidden" name="numtext" value="15.00" onchange="calculate(this.form)">
    <input type="hidden" name="numitem" value="pen" onchange="calculate(this.form)">
    <div class="box2">Rice and Beans $15.00</div>
    </div><br>
    
    <div class="box1">
    <input type="checkbox" name="num" onclick="calculate(this.form)">
    <input type="hidden" name="numtext" value="6.00" onchange="calculate(this.form)">
    <input type="hidden" name="numitem" value="paper" onchange="calculate(this.form)">
    <div class="box2">Chips and Salsa $6.00</div>
    </div><br>
    <div class="box1">
    <input type="submit" name="answer" value="Calculate Total" onclick="calculate(f)" />
    
    </div>
    </form>
    </body>
    </html>
    Last edited by outofcrayons; 07-19-2011 at 07:11 PM.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,629
    Thanks
    6
    Thanked 1,002 Times in 975 Posts
    Open the page in Firefox and then go on “Extras > Error console”. What does it tell you when you click the submit button on the page?

  • Users who have thanked VIPStephan for this post:

    outofcrayons (07-19-2011)

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Solved!

    I know not many responded but I did get help finding the solution...

    Code:
    <html>
    
    <head>
    
    <script type="text/javascript">
    
    var total = 0;
    
    function calculate(f) {
    
    	var nums = f.num;
    
    	var ntext = f.numtext;
    
    	var nitem = f.numitem;
    
    	var result = 0;
    
    	var items = '';
    
    	
    
    	for(var i=0;i<nums.length;i++) {
    
    		if(nums[i].checked)
    
    		{
    
    			result+=parseFloat(ntext[i].value);
    
    			items+=nitem[i].value+'\n';
    
    		}
    
    	}
    
    	
    
    	total = result
    
    
    
    }
    
    </script>
    
    
    
    </head>
    
    <body>
    
    <form name="myform">
    
    Build your own family meal<br>
    
    <div class="box1">
    
    <input type="checkbox" name="num" onClick="calculate(this.form)">
    
    <input type="hidden" name="numtext" value="24.00" onChange="calculate(this.form)">
    
    <input type="hidden" name="numitem" value="Chicken Enchiladas" onChange="calculate(this.form)">
    
    <div class="box2">Chicken Enchiladas $24.00</div>
    
    </div>
    
    <br>
    
    <div class="box1">
    
    <input type="checkbox" name="num" onClick="calculate(this.form)">
    
    <input type="hidden" name="numtext" value="15.00" onChange="calculate(this.form)">
    
    <input type="hidden" name="numitem" value="Rice and Beans" onChange="calculate(this.form)">
    
    <div class="box2">Rice and Beans $15.00</div>
    
    </div><br>
    
    <div class="box1">
    
    <input type="checkbox" name="num" onClick="calculate(this.form)">
    
    <input type="hidden" name="numtext" value="6.00" onChange="calculate(this.form)">
    
    <input type="hidden" name="numitem" value="Chips and Salsa" onChange="calculate(this.form)">
    
    <div class="box2">Chips and Salsa $6.00</div>
    
    </div><br>
    
    <div class="box1">
    
    Total <button type="button" onClick="alert('Your total is $' + total);">Show Total</button>
    
    </div>
    
    </form>
    
    </body>
    
    </html>


  •  

    Posting Permissions

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