reset password
Author Message
arodr856
Posts: 6
Posted 12:29 May 29, 2016 |

Can anybody help me with Lab 9. I'm having trouble getting java to add up checkbox values and also displaying all the input information after pushing the oncliick button .

karcini
Posts: 3
Posted 12:43 May 29, 2016 |

Those checkboxes are saved into variables as boolean values.  So it will save it as true or false depending on whether that button was pressed or not after evaluating your pizza.  So in the function you can add some methods that check whether or not the box has been checked.  You can additionally add a counter that is shared among all those checkbox methods.  So you can have that method check if it was checked, and if so increase the integer keeping count of the cost and increase the integer keeping count of how many toppings there were.

arodr856
Posts: 6
Posted 12:49 May 29, 2016 |

I'm not to sure what you mean. This is what I have at the moment 

<form>

      

<h3>Choose Pizza Toppings</h3>
            Cheese $3:<input type="checkbox" name="pizzaTopping" value="3"/>
            
            Pepperoni $3:<input type="checkbox" name="pizzaTopping" value="3"/>
            
            Mushrooms $3:<input type="checkbox" name="pizzaTopping" value="3"/>

            Green Peppers $3:<input type="checkbox" name="pizzaTopping" value="3"/>
            
            Pineapple $3:<input type="checkbox" name="pizzaTopping" value="3"/>
            

</form>

 

function myForm(){

var sum = 0
        var selectedTopping = document.forms["orderForm"]["pizzaTopping"];
        var i;
        for(i = 0; i < selectedTopping.length; i++) {
        if(selectedTopping[0].checked == false && selectedTopping[1].checked == false
            && selectedTopping[2].checked == false && selectedTopping[3].checked == false
            && selectedTopping[4].checked == false) {
                window.alert("Error: Please Select at least one topping!");
                break;
        }
        sum = selectedTopping.value;
        }
            

 

}

 

Last edited by arodr856 at 12:49 May 29, 2016.
Leggend
Posts: 5
Posted 13:11 May 29, 2016 |

So i was also having trouble getting the counter to work so i think i fixed it this is what i have so far 

(HTML)

 </select>
          <br>
          <p>Now pick some toppings!</p>
          <br>
          <input  id="1" type="checkbox" name="Toppings" onClick="return CountToppings()" value="Cheese"> Cheese<br>
          <input id="2" type="checkbox" name="Toppings" onClick="return CountToppings()" value="Pepperoni"> Pepperoni<br>
          <input id="3" type="checkbox" name="Toppings" onClick="return CountToppings()" value="Bacon"> Bacon<br>
          <input id="4" type="checkbox" name="Toppings" onClick="return CountToppings()" value="Meatballs"> Meatballs<br>
          <input id="5" type="checkbox" name="Toppings" onClick="return CountToppings()" value="Ham"> Ham<br>

(JS)

var Regularpizza = document.getElementById("Regular")
if (Regularpizza == true){ 
function CountToppings(){

var Counter = 0
if (document.getElementById("PizzaType").options[0]);
if (document.getElementById("1").checked = true); {
Counter = Counter + 1 }

if (document.getElementById("2").checked = true); {
Counter = Counter + 1 }

if (document.getElementById("3").checked = true); {
Counter = Counter + 1 }

if (document.getElementById("4").checked = true); {
Counter = Counter + 1 }
if (document.getElementById("5").checked = true); {
Counter = Counter + 1 }

tdang040888
Posts: 7
Posted 23:13 May 29, 2016 |

Anybody figured this one out?

TonyOs1
Posts: 13
Posted 23:25 May 29, 2016 |

You might be able to use a for loop.

Or you can just set a variable equal to 0, and if one of the checks gives back true, add one to it, for each one.

tdang040888
Posts: 7
Posted 21:23 Jun 03, 2016 |

I'm still trying to figure this one out. I'm using this as a reference point and I feel very close but I am lost once again.

http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_validate

 

Hints?