...

View Full Version : Resolved window.alert not working. Please help!



outofcrayons
07-18-2011, 07:29 PM
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?


<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>

VIPStephan
07-19-2011, 01:04 AM
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?

outofcrayons
07-19-2011, 05:01 PM
I know not many responded but I did get help finding the solution...


<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum