www.webdeveloper.com
Results 1 to 5 of 5

Thread: OnChange Function

  1. #1
    Join Date
    Dec 2013
    Posts
    2

    OnChange Function

    Hi All,

    I know this is a fairly noob question but I have not worked with JavaScript since I was in Uni
    Will make it up to you with addressing what I can in the PHP Selection

    But, really simply I have 3 fields to input numbers.
    When a number is entered I want JavaScript to supply a calculation based on this.
    Easy rite??

    The problem is that I cannot get JavaScript to write anything to the page So I am doing something wrong.
    I want JavaScript to act as details are entered, please help so that I can place data on the page.

    The code below is how far I have got, I am hoping the math is easy but I just need something to be printed to the page when a field is changed to start with,

    Code:
    <head>
    	<title>Mortgage Planning</title>
    </head>
    <body>
    	<div style="float: left; margin-left: 20px;">
    		<h3>Enter Saving Plan Here:</h3>
    		<table><tr><td>Weekly Saving:</td><td><input type="number" id="wkSav" onChange="calcTime()"></td></tr>
    		<tr><td>Existing Savings:</td><td><input type="number" id="exSav" onChange="calcTime()"></td></tr>
    		<tr><td><div style="height: 10px;"></td></tr> <!-- LINE BREAK -->
    		<tr><td>Purchase Price:</td><td><input type="number" id="price" onChange="calcTime()"></td></tr></table>
    	</div>
    	<div style="float: left; margin-left: 200px;">
    		<h2>Purchase Date Summary</h2>
    		<p id="summary">
    			<script type="text/javascript">
    				function calcTime() {
    					float con = document.getElementByID("wkSav");
    					float cur = document.getElementByID("exSav");
    					float pur = document.getElementByID("price");
    					
    					document.write(con);
    				}
    				
    			</script>
    		</p>	
    	</div>
    </body>
    I would be happy if this printed hello to the page at the moment
    Problem is that I get the page I want to start with but I am needing this to dynamically change as fields are complete.
    I have spent that last 5 hours on Google trying to make something happen and Firebug is recording the function request, just no processing of it

    Thanks in advance. Jarad

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Lightbulb

    I don't know what the calculations are supposed to do, so I just summed the values.

    Couple of problems with original script. Might want to check error console if you are using FF or Chrome browsers.

    1. float is not a definition in JS
    2. Access the contents using getElementById (not getElementByID)
    and provide an attribute of the element, ie value in this case for a textbox entry.
    3. document.write will reset script to initial load conditions every time it is called.
    4. Added some error checking for invalid entries.

    See modifications to your code below:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title>Mortgage Planning</title>
    </head>
    <body>
    <div style="float: left; margin-left: 20px;">
      <h3>Enter Saving Plan Here:</h3>
      <table>
       <tr><td>Weekly Saving:</td><td><input type="number" id="wkSav" onChange="calcTime()"></td></tr>
       <tr><td>Existing Savings:</td><td><input type="number" id="exSav" onChange="calcTime()"></td></tr>
       <tr><td><div style="height: 10px;"></td></tr> <!-- LINE BREAK -->
       <tr><td>Purchase Price:</td><td><input type="number" id="price" onChange="calcTime()"></td></tr>
      </table>
    </div>
    <div style="float: left; margin-left: 200px;">
      <h2>Purchase Date Summary</h2>
      <p id="summary"> </p>	
    </div>
    
    <script type="text/javascript">
    function calcTime() {
      var con = parseFloat(document.getElementById("wkSav").value) || 0;
      var cur = parseFloat(document.getElementById("exSav").value) || 0;
      var pur = parseFloat(document.getElementById("price").value) || 0;
      document.getElementById('summary').innerHTML = (con+cur+pur).toFixed(2);
    }
    </script>
    
    </body>
    </html>

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Quote Originally Posted by CRU-Arnie View Post
    Easy rite??
    its Right not Rite...


    Code:
    document.write(con)
    needs to be...
    Code:
    document.getElementById("summary");
    to access the p tag and your script should be in the <head> of the page.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Dec 2013
    Posts
    2
    Thank you JMRKER, You have put me on the correct path and I am working away.
    At the time of posting the calculations did nothing, it was just a test to get data onto the page so thank you.

    But 'localhost' / 'home' or whatever, thanks for the response I have just ignored it as I am unsure if you are trying to start an unrelated debate or quite what. (No response wanted thanks).

    This one is sorted out, I am just working through to make things happen now.
    I am surprised at how much of this I have forgotten in several years of not using it, time to learn again

    Have a great Christmas Everyone.

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    You're most welcome.
    Happy to help.
    Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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