<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 :)
Any discussion of JavaScript where you limit yourself to DOMs is stuck in the 90s.
* Flash's ActionScript is JavaScript without a DOM.
* Mozilla Rhino is a JavaScript engine for the JVM - no DOM.
* node.js which Caer mentioned is a servery thing without a DOM.
* Qt (the desktop GUI framework) uses JavaScript as a general purpose language.
* Unity (3D game engine) supports JavaScript for game programming.
There's plenty of others I wasn't even aware of - OpenOffice and Photoshop both allow scripting with it, and numerous other minor things all use it.
It's probably is still >99.9% used by browsers (even though I'm sure that's not a remotely scientific statistic :P ), but non-browser use has definitely been growing these past few years.