Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Smile Radio button Value in Text box?

    I have the following code which I want to make a selection and the value should reflect in the text box and the text box should take on that value for future calculations.

    Code:
    <!-- Row 13, Col 1 Order Value -->
    <tr><td colspan="2" align="right">Delivery Options: 
    <input type="radio" name="sapo" value="35" onclick="deliveryCost('35')" /> R35 - SA Post Office 
    <input type="radio" name="sapo" value="80" onclick="deliveryCost('80')" /> R80 - Speed Services 
    <input type="radio" name="sapo" value="150" onclick="deliveryCost('150')" /> R150 - Courier Services 
    </td>
    
    <!-- Row 13, Col 2 Order Value Box-->
    <td colspan="1" align="left"><input style="margin-left: 60px" type="text" name="delivery" size="10" readonly="readonly" />
    </td></tr>
    Last edited by hans_cellc; 10-05-2011 at 07:04 PM.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Rough locally code:
    Code:
    <input type="radio" name="sapo" value="35" onclick="delivery.value=this.value" /> R35 - SA Post Office 
    <input type="radio" name="sapo" value="80" onclick="delivery.value=this.value" /> R80 - Speed Services 
    <input type="radio" name="sapo" value="150" onclick="delivery.value=this.value" /> R150 - Courier Services
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    hans_cellc (10-05-2011)

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Thanks for your response, it works perfectly.
    One problem though how could I add a $ currency sign.

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    From Kor's code:
    Code:
    <input type="radio" name="sapo" value="35" onclick="delivery.value='$'+this.value" /> R35 - SA Post Office 
    <input type="radio" name="sapo" value="80" onclick="delivery.value='$'+this.value" /> R80 - Speed Services 
    <input type="radio" name="sapo" value="150" onclick="delivery.value='$'+this.value" /> R150 - Courier Services
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    hans_cellc (10-05-2011)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by hans_cellc View Post
    Thanks for your response, it works perfectly.
    One problem though how could I add a $ currency sign.
    Just put the $ sign before the radio button value - but be aware that the result is then a string value which cannot then be used in arithmetic calculations.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    hans_cellc (10-05-2011)

  • #6
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Great that works. Sorry to be a nag but it does not want to add that text box to my calculations. I also tried to parseFloat() the tDelivery but then it gives a NaN, I also tried parseInt() same Nan then With the exact code below it encapsulates the two totals printing eg: R150 R35.
    All the other boxes works perfectly just the last one to add the delivery.

    Code:
    // Function to calculate order value 
    function calculateValue(orders) {
    	var orderValue = 0;
     
    	// Run through all the product fields
    	for(var i = 0; i < orders.elements.length; ++i) {
     
    	// Get the current field
    	var formField = orders.elements[i];
     
    	// Get the fields name
    	var formName = formField.name;
    	
    	// Is it a "product" field?
            if (formName.substring(0,4) == "prod") {
     
    	// Items price extracted from name
    	var itemPrice = parseFloat(formName.substring(formName.lastIndexOf("_") + 1));
     
    	// Get the Quantity
    	var itemQuantity = parseInt(formField.value);
     
    	// Update the OrderValue
    	if(itemQuantity >= 0) {
    	orderValue += itemQuantity * itemPrice;
    	}
    	}
        }
    	
    	// Display the totals
        orders.totalExcl.value = "R " + orderValue.toLocaleString();
        
        // function to calculate VAT at 15% as required and total inclusive.
    	function calcTotals(oValue) {
    	var vat = oValue * .15;
    	var totalIncluding = oValue + vat;
    	var tDelivery = orders.delivery.value;
    	var theTotalOrder = totalIncluding + tDelivery;
    	orders.vat.value = "R " + vat.toLocaleString();
    	orders.totalIncl.value = "R " + totalIncluding.toLocaleString();
    	orders.totalOrder.value = "R " + theTotalOrder.toLocaleString();
    	}
    	return calcTotals(orderValue);
    }
    Code:
    <!-- Row 10, Col 1 Order Value exluding Vat -->
    <tr><td colspan="2" align="right">Order Value Excluding Vat:</td>
    
    <!-- Row 10, Col 2 Order Value exluding Vat Box-->
    <td colspan="1" align="left">
    <input name="totalExcl" type="text" style="margin-left: 60px" size="10" onfocus="this.form.elements[0].focus()" />
    </td></tr>
    
    <!-- Row 11, Col 1 Vat of 15% -->
    <tr><td colspan="2" align="right">Vat calculated at 15%:</td>
    
    <!-- Row 11, Col 2 Vat Value Box-->
    <td colspan="1" align="left"><input name="vat" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
    </td></tr>
    
    <!-- Row 12, Col 1 Total Order Value including Vat -->
    <tr><td colspan="2" align="right">Order Value Including Vat:</td>
    
    <!-- Row 12, Col 2 otal Order Value including Vat Box-->
    <td colspan="1" align="left"><input name="totalIncl" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
    </td></tr>
    
    <!-- Row 13, Col 1 Order Value -->
    <tr><td colspan="2" align="right">Delivery Options: 
    <input type="radio" name="sapo" value="35" onclick="delivery.value='R ' + this.value" /> R35 - SA Post Office 
    <input type="radio" name="sapo" value="80" onclick="delivery.value='R ' + this.value" /> R80 - Speed Services 
    <input type="radio" name="sapo" value="150" onclick="delivery.value='R ' + this.value" /> R150 - Courier Services 
    </td>
    
    <!-- Row 13, Col 2 Order Value Box-->
    <td colspan="1" align="left"><input name="delivery" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
    </td></tr>
    
    <!-- Row 14, Col 1 Total Order Value including Vat -->
    <tr><td colspan="2" align="right"><strong>Total Order Value:</strong></td>
    
    <!-- Row 14, Col 2 otal Order Value including Vat Box-->
    <td colspan="1" align="left"><input name="totalOrder" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
    </td></tr>
    Last edited by hans_cellc; 10-04-2011 at 07:41 PM.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    *** REPOSTED ***
    Answered in his repost.

    Sorry, guys...didn't see this thread or would have posted here instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hans_cellc (10-05-2011)


  •  

    Posting Permissions

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