www.webdeveloper.com
Results 1 to 2 of 2

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

Hybrid View

  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
    FL
    Posts
    7,377

    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



Recent Articles