www.webdeveloper.com
Results 1 to 2 of 2

Thread: Need help with radio inputs and javascript for school project

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    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>

  2. #2
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,931

    Lightbulb

    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>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.15652 seconds
  • Memory Usage 2,872KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • 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_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates