# Need help with radio inputs and javascript for school project

• 03-11-2013, 03:46 PM
pcn3rd51
Need help with radio inputs and javascript for school project
So we have to let the user input the weight of an item between 0 and 400. and depending on the weight the price per pound is different.

0 - 150 lbs = \$20
151 - 300 lbs = \$15
301 - 400 = \$10

On top of this, there are 3 radio buttons, 50% discount, 25% discount, and No Discount. I'm having trouble detecting which radio button the user has selecting and obtaining the value that i have put for it in the HTML. If somebody could guide me in the right direction, it would be greatly appreciated. Here is my JavaScript...

Code:

```<script type="text/javascript">        function shippingTotal() {                 var itemweight = parseInt( document.getElementById("itemweight").value );         var disc50 = document.getElementById("disc50").value;         var disc25 = document.getElementById("disc25").value;         var disc00 = document.getElementById("disc00").value;         var subtotal;         var weighttotal;         var discount;         var grandtotal;                 if ( itemweight < 0 || itemweight > 400 ) {                         msg = "<div id='alertmsg'>That weight is not accepted. Please try again.</div>";                 } // <- ends if below zero or over four hundred                 else { // <- first radio button                         if  ( disc50.checked = true ) {                                 if ( itemweight > 0 && itemweight <= 150 ) {                                                                 weighttotal = itemweight * 20;                                 subtotal = itemweight * disc50 * 20;                                 discount = ( disc50 / 100 ) * 20;                                 grandtotal = weighttotal - discount;                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                 msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                 msg = msg + "<div id='resmessage'>Discount rate is " + disc50 + "%</div>";                                 msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                 msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                         } // <- ends itemweight is between zero and one fifty                                                 else {                                                                 if ( itemweight > 150 && itemweight <= 300 ) {                                                                                 weighttotal = itemweight * 15;                                         subtotal = itemweight * disc50 * 15;                                         discount = ( disc50 / 100 ) * 15;                                         grandtotal = weighttotal - discount;                                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                         msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Discount rate is " + disc50 + "%</div>";                                         msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                                         } // <- ends itemweight is between one fifty and three hundred                                                                 else {                                                                                 weighttotal = itemweight * 10;                                         subtotal = itemweight * disc50 * 10;                                         discount = ( disc50 / 100 ) * 10;                                         grandtotal = weighttotal - discount;                                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                         msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Discount rate is " + disc50 + "%</div>";                                         msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                         } // <- ends itemweight between three hundred and four hundred                                         } // <- ends else about not being between zero and one fifty                                         } // <- ends else  between zero and four hundred                         else { // <- second radio button                                         if  ( disc25.checked = true ) {                                 if ( itemweight > 0 && itemweight <= 150 ) {                                                                 weighttotal = itemweight * 20;                                 subtotal = itemweight * disc25 * 20;                                 discount = ( disc25 / 100 ) * 20;                                 grandtotal = weighttotal - discount;                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                 msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                 msg = msg + "<div id='resmessage'>Discount rate is " + disc25 + "%</div>";                                 msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                 msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                         } // <- ends itemweight is between zero and one fifty                                                 else {                                                                 if ( itemweight > 150 && itemweight <= 300 ) {                                                                                 weighttotal = itemweight * 15;                                         subtotal = itemweight * disc25 * 15;                                         discount = ( disc25 / 100 ) * 15;                                         grandtotal = weighttotal - discount;                                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                         msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Discount rate is " + disc25 + "%</div>";                                         msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                                         } // <- ends itemweight is between one fifty and three hundred                                                                 else {                                                                                 weighttotal = itemweight * 10;                                         subtotal = itemweight * disc25 * 10;                                         discount = ( disc25 / 100 ) * 10;                                         grandtotal = weighttotal - discount;                                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                         msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Discount rate is " + disc25 + "%</div>";                                         msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                         } // <- ends itemweight between three hundred and four hundred                                         } // <- ends else about not being between zero and one fifty                                         } // <- ends else  between zero and four hundred                                         } // <- ends second radio button else                                 else { // <- third radio button                                         if  ( disc00.checked = true ) {                                 if ( itemweight > 0 && itemweight <= 150 ) {                                                                 weighttotal = itemweight * 20;                                 subtotal = itemweight * 20;                                 discount = 0;                                 grandtotal = weighttotal - discount;                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                 msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                 msg = msg + "<div id='resmessage'>Discount rate is " + disc00 + "%</div>";                                 msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                 msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                         } // <- ends itemweight is between zero and one fifty                                                 else {                                                                 if ( itemweight > 150 && itemweight <= 300 ) {                                                                                 weighttotal = itemweight * 15;                                         subtotal = itemweight * 15;                                         discount = 0;                                         grandtotal = weighttotal - discount;                                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                         msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Discount rate is " + disc00 + "%</div>";                                         msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                                         } // <- ends itemweight is between one fifty and three hundred                                                                 else {                                                                                 weighttotal = itemweight * 10;                                         subtotal = itemweight * 10;                                         discount = 0;                                         grandtotal = weighttotal - discount;                                                                                 msg = "<div id='resmessage'>Total weight is " + itemweight + " pounds</div>";                                         msg = msg + "<div id='resmessage'>Weight cost is \$" + weighttotal.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Discount rate is " + disc00 + "%</div>";                                         msg = msg + "<div id='resmessage'>Discount amount is \$" + discount.toFixed(2);  + "</div>";                                         msg = msg + "<div id='resmessage'>Total cost is \$" + grandtotal.toFixed(2);  + "</div>";                                                         } // <- ends itemweight between three hundred and four hundred                                         } // <- ends else about not being between zero and one fifty                                         } // <- ends else  between zero and four hundred                                         } // <- ends third radio button else                                 }// <- ends else about the itemweight being between zero and four hundrred                 document.getElementById("results").innerHTML = msg;                 } // <-ends function shippingTotal </script>```
• 03-11-2013, 07:55 PM
JMRKER
Seems like you could figure the discounts a bit easier by modifying this bit of code...
Code:

```<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title> Untitled </title> </head> <body> <input type="text" value="100" id="quantity">Quantity<br> <input type="radio" name="RBtn" value="0" onclick="calcResults(this.value)"> No discount<br> <input type="radio" name="RBtn" value=".25" onclick="calcResults(this.value)"> 25% discount<br> <input type="radio" name="RBtn" value=".50" onclick="calcResults(this.value)"> 50% discount<br> <input type="text" value="" id="discount" readonly> Amount of discount<br> <input type="text" value="" id="cost" readonly> Final Cost <script type="text/javascript"> function calcResults(info) {   var pcent = Number(info);   var amt = document.getElementById('quantity').value;  // may want to add some input error checks to this   var discount = amt*pcent;   document.getElementById('discount').value = discount.toFixed(2)   document.getElementById('cost').value = (amt-discount).toFixed(2); } </script> </body> </html>```
However, if you REALLY, REALLY, REALLY need a radio button status check function, consider this...
Code:

```<script type="text/javascript"> function getRBtnName(GrpName) {   var sel = document.getElementsByName(GrpName);   var fnd = -1;   var str = '';   for (var i=0; i<sel.length; i++) {     if (sel[i].checked == true) { str = sel[i].value;  fnd = i; }   } //  return fnd;  // return option index of selection // comment out next line if option index used in line above    return str; } </script>```

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09607 seconds
• Memory Usage 2,432KB
• Queries Executed 11 (?)
Template Usage (20):
• (3)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates