lovephp Posted October 5, 2012 Share Posted October 5, 2012 <!DOCTYPE html> <html> <head> <script type="text/javascript"> function DisplayPrice(price){ var val = 0; for( i = 0; i < document.form.price.length; i++ ){ if( document.form.price[i].checked == true ){ val = document.form.price[i].value; } } var parseInt(val); document.getElementById('totalSum').value=sum; } </script> </head> <body> Choose another number:<br> <form name="form2" id="form" runat="server"> <br> <input id="rdo_1" type="radio" value="15" name="price" onclick="DisplayPrice(this.value);">15 <br> <input id="rdo_2" type="radio" value="87" name="price" onclick="DisplayPrice(this.value);">87 <br> </form> $<input type="text" name="totalSum" id="totalSum" value="" size="2" readonly="readonly"> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/269124-price-not-being-printed-help-please/ Share on other sites More sharing options...
lovephp Posted October 5, 2012 Author Share Posted October 5, 2012 actually it does not have to do a sum just print which radio is selected but im unable to get it Quote Link to comment https://forums.phpfreaks.com/topic/269124-price-not-being-printed-help-please/#findComment-1382909 Share on other sites More sharing options...
Christian F. Posted October 5, 2012 Share Posted October 5, 2012 You need to go over your code once more I think, as there's some lapses in your logic. If you translate what it does, line-by-line, into plain English you should be able to spot it yourself. Remember to always keep track of what you have, and what has been done, though. PS: Please use proper punctuation so that we don't have to decipher your posts to understand what you're writing. We are, after all, spending our own free time without getting paid a single cent, so by making it as easy as possible for us to help you'll increase your own chances to actually get help. Quote Link to comment https://forums.phpfreaks.com/topic/269124-price-not-being-printed-help-please/#findComment-1382946 Share on other sites More sharing options...
codefossa Posted October 6, 2012 Share Posted October 6, 2012 Here's an example with checkboxes that will sum the values together in a span tag. It should give you enough of an example for both ways you may want to do it, whether it sums together or not. On a side note. If you're really puttin' much together. JQuery would make your JS quite a bit faster to write, smaller (not counting the include of JQ), and easier to work with. <html> <head> <script type="text/javascript"> window.onload = function() { var btn = window.document.getElementsByClassName("checkbox"); for (var i in btn) { if (btn[i].value != undefined) { btn[i].checked = 0; btn[i].onclick = function() { var total = 0; for (var x in btn) { if (btn[x].checked != undefined) { if (btn[x].checked == 1) { total += parseInt(btn[x].value * 100); } window.document.getElementById("totalSum").innerHTML = '$' + Math.round(total) / 100; } } } } } } </script> </head> <body> Choose another number:<br /><br /> <form name="form2" id="form" method="post" action="#"> <input class="checkbox" type="checkbox" value="15.30" name="price" />15.30<br /> <input class="checkbox" type="checkbox" value="87.24" name="price" />87.24<br /> <input class="checkbox" type="checkbox" value="13.278" name="price" />13.278<br /> <input class="checkbox" type="checkbox" value="82.964" name="price" />82.964<br /> </form> <b>Total:</b> <span id="totalSum">$0</span> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/269124-price-not-being-printed-help-please/#findComment-1383161 Share on other sites More sharing options...
codefossa Posted October 6, 2012 Share Posted October 6, 2012 Not sure if recreating the function for each element is bad or not, so you may want to just set the onclick with a loop to the function. In JQuery you would just use $(".class").click(function() { }); which I assume does the same as I did above, but I'll let someone else comment on whether that's wrong to do or not. Quote Link to comment https://forums.phpfreaks.com/topic/269124-price-not-being-printed-help-please/#findComment-1383162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.