Jump to content

Using Javascript with Checkboxes and Radio Buttons


proggR

Recommended Posts

I'm trying to make a pizza ordering form and I want the user to be able to select a radio button of a type of pizza and it will loop through all the toppings checkboxes and leave only the toppings applicable to that type of pizza checked. Furthermore, unless the "custom" radio button is selected, no changes can be made to the checkboxes.

I have the radio button call a function onclick and the function gets passed the type's name. I can see, through alerts, that the function is receiving the type successfully. From here I kind of get stuck because I'm not sure how to specify individual checkboxes. The pseudo code would look like.

If type equals hawaiin then
uncheck all checkboxes of name topping
individually check checkbox of name topping who's value is bacon
individually check checkbox of name topping who's value is ham
individually check checkbox of name topping who's value is pineapple
lock checkboxes

more ifs for other types

if type equals custom
clear all checkboxes of name topping
unlock checkboxes

I'm just not sure how to go about doing that. If anyone could point me in the right direction it would really appreciated. Thank you in advance.

couple of examples to get you going. replace the stuff in quotes with your relevant names.

 

also, i suggest you give all of the checkboxes unique names, otherwise you can't pick and choose which ones to select/enable since they will be mutually exclusive

 

<script type="text/javascript">
function selectBoxes() {
   if(document."formname".getElementById("type").value=='hawaiian') {
      document."formname"."checkboxname".checked = true;
      document."formname"."checkboxname".checked = false;
   }
}

function enableBox() {
   document."formname"."checkboxname".disabled = false;
}

I need them to be all of the same name so when it gets passed to my PHP script it can loop through them and add a certain value to the price. The price per topping changes depending on the size of the pizza so I can't  just use it as the value either. I'll try out the code you have though. Thank you :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.