SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy JS: automatic calculate without click any button

    halo..i got some javascript problem..i need some help.
    i have a hidden form value which contain of the TOTAL., let say the total is rm100.
    then i have anohter two radio button and two text box, one radio button for one text box.
    for the first radio button & textbox set, the user should insert the percentage; for the second radio button and terxt box set, the user should insert the ringgit malaysia.
    then how can i get the value form the hidden and the text box and radio button, it will automatically calcualte the discount rate witout submit any button.
    let say i have the hidden total is rm100, then i choose ringgit radio button and insert rm50 into the text box, then below there will display rm50(rm100-rm50)?..plz help..plz guide me some coding..thanx.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Dont think I totally understand what you need purple83 but the following script does I think what you need.
    Code:
    <script type="text/javascript">
    
    	function calculateTotal() {
    		
    		var totalAmt = document.addem.total.value;
    		totalR = eval(totalAmt - document.addem.tb1.value);
    		
    		document.getElementById('update').innerHTML = totalR;
    	}
    
    </script>
    
    <form name="addem" action="" id="addem" >	
    	<span id="update">100</span>
    	<p><input type="text" name="tb1" onkeyup="calculateTotal()"/>first textbox</p>
    	
    	<input type="hidden" name="total" value="100" />
    </form>
    See if thats what you need
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi spikeZ,,,..yaya..thanx alot..almost same as wat i wan..thank you very much...
    but if i wan to click the radio button first and then calcualte how? how if i got two kind of text box,? one is ringigt discount, one is percentage discount? thanx

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Extend the function to see which radio button is selected and use that value as required.

    Code:
    <script type="text/javascript">
    
    	function calculateTotal() {
    		
    		var totalAmt = document.addem.total.value;
    
    		var val = 0;
    		
    		for( i = 0; i < document.addem.discount_type.length; i++ ) {
    		if( document.addem.discount_type[i].checked == true )
    		val = document.addem.discount_type[i].value;
    		}
    		
    		if(val == 'ring') {
    			dType = document.addem.tb1.value;
    		} else {
    			dType = document.addem.tb2.value;
    		}
    		
    		totalR = eval(totalAmt - dType);
    		
    		document.getElementById('update').innerHTML = totalR;
    	}
    
    </script>
    
    <form name="addem" action="" id="addem" >	
    	<span id="update">100</span>
    	<p><input type="radio" name="discount_type" value="ring" checked > Ring: <input type="text" name="tb1" onkeyup="calculateTotal()"/>first textbox</p>
    	<p><input type="radio" name="discount_type" value="percentage" > Percentage: <input type="text" name="tb2"  onkeyup="calculateTotal()"/>Second textbox</p>
    	
    	<input type="hidden" name="total" value="100" />
    </form>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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