<HTML> <HEAD> <script type="text/javascript"> //THIS IS THE FUNCTION FOR MANIPULATING CHECKBOXES AND PRICING function calcUS(){ // Gathers field information var f1 = (document.listofoptions.elements['Grp1'].value); var jb = (document.listofoptions.elements['Grp1'].value); document.displayForm.elements['JBTest'].value = jb; document.displayForm.elements['F1Test'].value = f1; } //END OF CHECKBOX FUNCTION </script> </HEAD> <BODY> <form name="listofoptions"> <TABLE> <TR> <td width= 50%> Check1 </td> <td width= 10%> <input type="radio" name="Grp1" value="Check1" onclick="calcUS()"> </td> </tr> <tr> <td width= 50%> Check2 </td> <td width= 10%> <input type="radio" name="Grp1" value="Check2" onclick="calcUS()"> </td> </tr> </TABLE> </form> <form name="displayForm"> <TABLE> <TR> <td width= 50%> <input type="textbox" readonly style="background: #DDD; color: #000;" name="JBTest" value="" style="width:80px"> </td> </tr> <TR> <td width= 50%> <input type="textbox" readonly style="background: #DDD; color: #000;" name="F1Test" value="" style="width:80px"> </td> </tr> </TABLE> </form> </BODY> </HTML>
D'oh - can't believe I missed that...
DOM?
It works fine for checkboxes, so I assumed it would work for a radio button too.
But basically, I want to have 4 radio buttons, and store Check1, Check2 etc. in a variable according to which one is selected.
How would I modify my code to use your suggestions to do that?
I'm doing it this way so that there's no need to submit the form.
There's a group of radio buttons for choosing from selected options, then a group of checkboxes for adding further options.
There are prices associated with each option, and then there's a 'total' box that keeps a running total without having to repeatedly press a submit button.
<HTML> <HEAD> <script type="text/javascript"> //THIS IS THE FUNCTION FOR MANIPULATING CHECKBOXES AND PRICING function calcUS(){ // Gathers field information var f1 = document.getElementById['Check1']; var jb = (document.listofoptions.elements['Grp1'].value); var f2 = (document.listofoptions.elements['Check3'].checked); //Posts results document.displayForm.elements['JBTest'].value = jb; document.displayForm.elements['F1Test'].value = f1; document.displayForm.elements['F2Test'].value = f2; } //END OF CHECKBOX FUNCTION </script> </HEAD> <BODY> <form name="listofoptions"> <TABLE> <TR> <td width= 50%> Check1 </td> <td width= 10%> <input type="radio" name="Grp1" id="Check1" value="Check1" onclick="calcUS()"> </td> </tr> <tr> <td width= 50%> Check2 </td> <td width= 10%> <input type="radio" name="Grp1" id="Check2" value="Check2" onclick="calcUS()"> </td> </tr> <tr> <td width= 50%> Check2 </td> <td width= 10%> <input type="checkbox" name="Grp1" id="Check3" value="Check3" onclick="calcUS()"> </td> </tr> </TABLE> </form> <form name="displayForm"> <TABLE> <TR> <td width= 50%> <input type="textbox" readonly style="background: #DDD; color: #000;" name="JBTest" value="" style="width:80px"> </td> </tr> <TR> <td width= 50%> <input type="textbox" readonly style="background: #DDD; color: #000;" name="F1Test" value="" style="width:80px"> </td> </tr> <TR> <td width= 50%> <input type="textbox" readonly style="background: #DDD; color: #000;" name="F2Test" value="" style="width:80px"> </td> </tr> </TABLE> </form> </BODY> </HTML>
<HTML> <HEAD> <script type="text/javascript"> //THIS IS THE FUNCTION FOR MANIPULATING CHECKBOXES AND PRICING function calcUS(){ // Gathers field information var f2 = (document.listofoptions.elements['Check3'].checked); { for (var i=0; i < document.listofoptions.Grp1.length; i++) { if (document.listofoptions.Grp1[i].checked) { var rad_val = document.listofoptions.Grp1[i].value; } } //Posts results document.displayForm.elements['JBTest'].value = rad_val; document.displayForm.elements['F2Test'].value = f2; }} //END OF CHECKBOX FUNCTION </script> </HEAD> <BODY> <form name="listofoptions"> <TABLE> <TR> <td width= 50%> Check1 </td> <td width= 10%> <input type="radio" name="Grp1" id="Check1" value="Check1" onclick="calcUS()"> </td> </tr> <tr> <td width= 50%> Check2 </td> <td width= 10%> <input type="radio" name="Grp1" id="Check2" value="Check2" onclick="calcUS()"> </td> </tr> <tr> <td width= 50%> Check2 </td> <td width= 10%> <input type="checkbox" name="Check3" value="Check3" onclick="calcUS()"> </td> </tr> </TABLE> </form> <form name="displayForm"> <TABLE> <TR> <td width= 50%> <input type="textbox" readonly style="background: #DDD; color: #000;" name="JBTest" value="" style="width:80px"> </td> </tr> <TR> <td width= 50%> <input type="textbox" readonly style="background: #DDD; color: #000;" name="F1Test" value="" style="width:80px"> </td> </tr> <TR> <td width= 50%> <input type="textbox" readonly style="background: #DDD; color: #000;" name="F2Test" value="" style="width:80px"> </td> </tr> </TABLE> </form> </BODY> </HTML>
<script src="jquery.js"></script> <script type="text/javascript"> function calcUS() { var f2 = $('[name=Check3]').is(':checked') var rad_val = $('[name=Grp1]:selected').val() $('[name=JBTest]').val( rad_val ) $('[name=F2Test]').val( f2 ) } </script>
$(this).parent('tr').css('background','red')
Maybe if this was the start of something I was going to use a lot, but it's for a one off thing that I'm doing.
Anyways, I got it all sorted and although it's cumbersome code, it does work!
for (var i=0; i < document.listofoptions.Grp1.length; i++) { if (document.listofoptions.Grp1[i].checked) { var rad_val = document.listofoptions.Grp1[i].value; } }
var radGrp = document.listofoptions.Grp1, i = 0, len = radGrp.length, radVal; while (!radVal && i < len) { if (radGrp[i].checked) { radVal = radGrp[i].value; } i += 1; }This way you're cacheing the DOM element names, so they don't need to be looked up for each loop iteration, and also the conditions of the loop mean you don't continue to loop once you've found the value you want. It also saves you some typing if you need to do more stuff with the elements within the loop :)