Results 1 to 4 of 4

Thread: variable form fields with javascript

  1. #1

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    variable form fields with javascript

    Guys

    I need to determine which textbox to return a value to using javascript.

    In the code below, I need the value 'premtype' to be either prem or alt_prem depending on the radio button selected. I just can't seem to figure out how to pass the variable into the 'document.frm.variablehere.value' statement:

    Code:
    //check if renewal premium or alternative renewal premium has been chosen....
    
    if (document.frm.choice[0].checked)//renewal premium
    	{
    		total = document.frm.prem.value
    		premtype = "prem"
                 }
    else
    	{//alt renewal premium
    		total = document.frm.alt_prem.value
    		premtype = "alt_prem"
    	}	
    
    //work out premiums.......		
    	if (stmt == true)//checkbox checked....
    		{
    			//calculate the new premium amount
    			total = parseFloat(total) + parseFloat(changeBy);
    			//populate premium field
    			document.frm.premtype.value = Math.abs(total).toFixed(2);
    		}
    	else//checkbox unchecked....
    		{
    			//calculate the new premium amount
    			total = parseFloat(total) - parseFloat(changeBy);
    			//populate 'prem' field
    			document.frm.premtype.value = Math.abs(total).toFixed(2);			
    		}

    I hope this makes sense!

    Cheers
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: variable form fields with javascript

    does this work? Not sure where your getting changedBy value from.

    Code:
    function NewPremiumAmount(amt,change)
    {
      //calculate the new premium amount
      amt = parseFloat(amt) + parseFloat(change);
      //populate premium field
      return Math.abs(amt).toFixed(2);	
    }
    
    if (document.frm.choice[0].checked)	{
    document.frm.prem.value = NewPremiumAmount(document.frm.prem.value,changedBy);
    }
    else{
       document.frm.prem.value = NewPremiumAmount(document.frm.alt_prem.value,changedBy);
    }
    Last edited by DeadEyes; Jun 30th, 2005 at 01:23 PM.

  3. #3
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    Re: variable form fields with javascript

    try this:
    HTML Code:
    <html>
    <script type="text/javascript">
    //check if renewal premium or alternative renewal premium has been chosen....
    function checkit(){
    //check if renewal premium or alternative renewal premium has been chosen....
    changeBy = "hi";
    if (document.frm.choice.checked == true)//renewal premium
    	{
    		total = document.frm.prem.value
    		premtype = "prem"
        }
    else
    	{//alt renewal premium
    		total = document.frm.alt_prem.value
    		premtype = "alt_prem"
    	}	
    
    //work out premiums.......		
    	if (document.frm.choice.checked == true)//checkbox checked....
    		{
    			//calculate the new premium amount
    			total = parseFloat(total) + parseFloat(changeBy);
    			//populate premium field
    			document.frm.premtype.value = Math.abs(total).toFixed(2);
    		}
    	else//checkbox unchecked....
    		{
    			//calculate the new premium amount
    			total = parseFloat(total) - parseFloat(changeBy);
    			//populate 'prem' field
    			document.frm.premtype.value = Math.abs(total).toFixed(2);			
    		}
    }
    </script>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 15</title>
    </head>
    
    <body>
    
    <form name="frm" method="POST">
    <input id="R1" type="radio" value="V1" name="choice">
    <input type="button" onclick="checkit();" value="Button" name="B1"><input type="text" name="prem" size="20"><input type="text" name="premtype" size="20"><input type="text" name="alt_prem" size="20"></form>
    <p id="t">hi</p>
    <p onclick="checkit();">asd</p>
    </body>
    
    </html>
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  4. #4

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    Re: variable form fields with javascript

    OK, the snippet of code you see above is just a part of a larger function where changeBy and certain other variables are set. The function is called by a onChange event in a number of checkbox fields in an asp page.

    DeadEyes, I don't want to do it that way because this is only one of a number of fields that I want to apply this to so I want to set all of the variables in the one test
    Code:
    if (document.frm.choice[0].checked)
    All, I'm struggling to see what you've changed from the code that I posted.

    Perhaps if I post the whole code it will be clearer:

    Code:
    function doRenewal(obj){
    var x = (obj.id)
    var changeBy = parseFloat(eval("document.frm."+x+".value"))
    var check = ("document.frm."+x)	
    var stmt = eval("document.frm."+x+".checked")
    var total = document.frm.prem.value	
    var altval
    var remaining
    var premtype
    
    //this section determines how much should be added for Breakdown, Uninsured Loss
    //& Homestart depending on whether or not Breakdown, Uninsured Loss is ticked....
    
    	if ((x == "BUHOption") && (stmt == true))//if BUHOption checked....
    		{	//If BUOption also checked....
    			if (document.frm.BUOption.checked == true)	
    				{	//cost for BUHOption is only £19
    					changeBy = 19;
    				}
    		}
    	else
    		{
    			if ((x == "BUHOption") && (stmt == false))//if BUHOption UNchecked....
    			{
    				if (document.frm.BUOption.checked == true)
    					{
    						changeBy = 19;
    					}
    			}
    		}
    
    		
    	if ((x == "BUOption") && (stmt == true))//if BUHOption checked.....
    		{//If BUHOption also checked....
    			if (document.frm.BUHOption.checked == true)
    				{//no extra charge...
    					changeBy = 0;
    				}
    		}	
    	else
    		{
    			if ((x == "BUOption") && (stmt == false))//if BUHOption UNchecked....
    				{//If BUHOption also checked....
    					if (document.frm.BUHOption.checked == true)
    						{
    							changeBy = 0;
    						}
    				}
    		}	
    
    
    //check if renewal premium or alternative renewal premium has been chosen....
    
    if (document.frm.choice[0].checked)//renewal premium
    	{
    		total = document.frm.prem.value
    		premtype = "prem"
    		alert(premtype)
    	}
    else
    	{//alt renewal premium
    		total = document.frm.alt_prem.value
    		premtype = "alt_prem"
    		alert(premtype)
    	}	
    
    	
    
    
    //work out premiums.......		
    	if (stmt == true)//checkbox checked....
    		{
    			//calculate the new premium amount
    			total = parseFloat(total) + parseFloat(changeBy);
    			//populate premium field
    			document.frm.premtype.value = Math.abs(total).toFixed(2);
    			//alert(prem)
    		}
    	else//checkbox unchecked....
    		{
    			//calculate the new premium amount
    			total = parseFloat(total) - parseFloat(changeBy);
    			//populate 'prem' field
    			document.frm.premtype.value = Math.abs(total).toFixed(2);			
    			//alert(prem)
    		}	
    		
    //work out installments.....				
    		if (parseFloat(total) <= 150)//Use installment calculator 'K'
    			{
    				//deposit is 60% of total premium - populate 'dep' field
    				document.frm.dep.value = Math.abs((60/100)*total).toFixed(2);
    				//there will be 1 further payment - populate 'times' field
    				document.frm.times.value = 1;
    				//the remainder is the total minus the deposit
    				remaining = Math.abs((total - document.frm.dep.value)).toFixed(2);
    				//plus 12% - populate 'remainder' field
    				document.frm.remainder.value = Math.abs((parseFloat(remaining)+((12/100))*parseFloat(remaining))).toFixed(2);
    				//total is deposit plus remainder - populate 'tot' field
    				document.frm.tot.value = (parseFloat(document.frm.dep.value)+parseFloat(document.frm.remainder.value)).toFixed(2);
    			}
    		else
    			{
    				if ((parseFloat(total) > 150) && (parseFloat(total) <= 200))//Use istallment calculator 'L'
    					{
    						//deposit is 40% of total premium - populate 'dep' field
    						document.frm.dep.value = Math.abs((40/100)*total).toFixed(2);
    						//there will be 2 further payments - populate 'times' field
    						document.frm.times.value = 2
    						//the remainder is the total minus the deposit/2
    						remaining = Math.abs(((total - document.frm.dep.value))/2).toFixed(2);
    						//plus 12% - populate 'remainder' field
    						document.frm.remainder.value = Math.abs((parseFloat(remaining)+((12/100))*parseFloat(remaining))).toFixed(2);
    						//total is deposit plus (remainder*2) - populate the 'dep' field
    						document.frm.tot.value = (parseFloat(document.frm.dep.value)+parseFloat(document.frm.remainder.value)*2).toFixed(2);					
    					}
    
    					else
    						{
    							if (parseFloat(total) > 200)//Use installment calculator 'O'
    								{
    									//deposit is 40% of total premium - populate 'dep' field
    									document.frm.dep.value = Math.abs((40/100)*total).toFixed(2);
    									//there will be 3 further payments - populate 'times' field
    									document.frm.times.value = 3
    									//the remainder is the total minus the deposit/3
    									remaining = Math.abs(((total - document.frm.dep.value))/3).toFixed(2);
    									//plus 12% - populate 'remainder' field
    									document.frm.remainder.value = Math.abs((parseFloat(remaining)+((12/100))*parseFloat(remaining))).toFixed(2);									
    									//total is deposit plus (remainder*3) - populate 'dep' field
    									document.frm.tot.value = (parseFloat(document.frm.dep.value)+parseFloat(document.frm.remainder.value)*3).toFixed(2);		
    								}
    						}
    				}	
    
    }
    
    //The end....
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

Posting Permissions

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



Click Here to Expand Forum to Full Width