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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How do I get javascript to automatically fill out a form?

    Dear all,

    I have been developing for the past few weeks a little script that calculates monthly payments (just like a financial calculator). Up to now, it works for payments that are linear (aka, equal payments from month one to month X) but I am trying to add the idea of say accelerated payments to the script. In other words, I want my script to calculate the monthly payment from month 1 to say 12 until the future value reaches the amount I specified, then from month X to the last month until it reaches the future value I specified.

    Without going any further, here is my whole script. As you will see, at the bottom of the script there is a form. Then, the calculator kicks into action and calculates the monthly payment with future value:

    Code:
    <html>
    
    <head>
    <title>Calculatrice</title>
    <script type='text/javascript' src='jquery.js?ver=1.3.2'></script>
    <script type='text/javascript'>
    		$(document).ready(function () {
     
    			function showTab( name ) 
    			{
      				name = '#' + name;
    
      				$('div').not(name).hide();
    
      				$(name).show();
    			}
     
    			$('#dropdown').change( function() {
    
      				showTab( $( this ).val() );
    			});
     
    			showTab( $('#dropdown').val() );
     
    		});  
    </script>
    	
    </head>
    <body>
    
    
    
    
    
    
    
    <script type="text/javascript"><!--
    
    /*
    Texte pour les remboursements accélérés
    */
    
    
    function PMT(i, n, p, f) {
    		var pvif = Math.pow(1 + +i, +n);
    		var pmt = i / (pvif - 1) * -(p * pvif + +f);
    		
    		return pmt;
    
    }
    
    
    
    
    function CalculatePMTFromForm(idFacteur, idLoanAmount, idPremierVersement, idFuture, idFutureMois, idAnnualInterestRate, idMonths, idResult) {
     
     var b = jQuery('#' + idAnnualInterestRate).val()/100/365*30.875;
     var n = jQuery('#' + idFutureMois).val();
     var m = jQuery('#' + idFacteur).val();
     var i = b*m;
     var f = jQuery('#' + idFuture).val();
     var pvers = jQuery('#' + idPremierVersement).val();
     var ps = jQuery('#' + idLoanAmount).val();
     var p = ps-pvers
     var pmt = PMT(i, n, -p, f);
     jQuery('#' + idResult).val(pmt.toFixed(2));
    }
    function performCalc() {
     CalculatePMTFromForm('Facteur', 'LoanAmount', 'PremierVersement', 'Future', 'FutureMois', 'InterestRate', 'Months', 'Payment');
    }
    jQuery(document).ready(function() { performCalc(); jQuery('.calc').keyup(performCalc); });
    // --></script></p>
    
    
    
    
    
    
    
    
    
    <style type="text/css">
    .entry .labeledform { text-align: left; }
    .entry .labeledform label { width: 120px; text-align: right; float: left; padding-right: 10px; }
    .entry .labeledform div { clear: left; }
    </style>
    
    
    
    <form class="labeledform">
    <div><label for="Facteur">Facteur de multiplication</label></p>
    <input id="Facteur" class="calc" size="7" type="text" value="30.8075" /> 	</div><br>
    <div><label for="LoanAmount">Montant du pr&ecirc;t:</label></p>
    <input id="LoanAmount" class="calc" size="7" type="text" value="145000" /> $</div><br>
    <div><label for="PremierVersement">Premier Versement:</label></p>
    <input id="PremierVersement" class="calc" size="7" type="text" value="0" /> $</div><br>
    <div><label for="Future">Remboursement acc&eacute;l&eacute;r&eacute;s pour atteindre</label></p>
    <input id="Future" class="calc" size="7" type="text" value="3" />$ apr&#232;s <input id="FutureMois" class="calc" size="7" type="text" value="3" /> mois <FONT COLOR="red" >Mettre r aux deux endroits s'il n'y a pas de remboursement acc&eacute;l&eacute;r&eacute; </FONT>	</div><br>
    <div><label for="InterestRate">Taux d'int&eacute;r&ecirc;t:</label></p>
    <input id="InterestRate" class="calc" size="7" type="text" value="9" /> %</div><br>
    <div><label for="Months">Nombre de mois:</label></p>
    <input id="Months" class="calc" size="7" type="text" value="60" /> mois</div><br>
    <div><label for="OptionAchat">Option d'achat</label></p>
    <input id="OptionAchat" class="calc" size="7" type="text" value="10000" /> $</div><br>
    <div><label for="Payment">Loyer acc&eacute;l&eacute;r&eacute;:</label></p>
    <input id="Payment" size="7" type="text" value="0" /> $</div><br>
    <div><label for="Payment2">Loyer 2e partie:</label></p>
    <input id="Payment2" size="7" type="text" value="0" /> $</div><br>
    </div><br>
    </form>

    </body>
    </html>


    Regarding my problem, I've tried adding a second PMT() function to calculate the second part, which actually works. I came up with the right numbers and everything but I don't know how to automatically show the result in the Payment2 field. However, I don't know how to make replicate the Payment field in the form, which automatically shows the payment.

    TL;DR: How do I replicate the Payment field in the form that automatically shows the result of a calculation (function PMT()) but with a different variable?

    Thanks!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Marc312 View Post
    Regarding my problem, I've tried adding a second PMT() function to calculate the second part, which actually works. I came up with the right numbers and everything but I don't know how to automatically show the result in the Payment2 field. However, I don't know how to make replicate the Payment field in the form, which automatically shows the payment.

    TL;DR: How do I replicate the Payment field in the form that automatically shows the result of a calculation (function PMT()) but with a different variable?

    Thanks!
    You just have to specify $('#Payment2').val(xxxxx) or pass 'Payment2' if you are passing the id as you are doing to CalculatePMTFromForm(). If this doesn't work, then you need to post the function you added for calculating the second part.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • Users who have thanked glenngv for this post:

    Marc312 (03-11-2014)

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Wow. That was easy. Thanks glennv!


  •  

    LinkBacks (?)

    1. 03-11-2014, 02:39 PM

    Posting Permissions

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