www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Simple javascript fails...7

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Posts
    65

    resolved [RESOLVED] Simple javascript fails...7

    hey,

    Script
    Code:
    function berekening(){
    	var beginkapitaal=document.getElementById("beginkapitaal").value;
    	var intrest=document.getElementById("intrest").value;
    	var aantal_jaar=document.getElementById("aantal_jaar").value;
    	var eindkapitaal=document.getElementById("eindkapitaal").value;
    
    	eindkapitaal=beginkapitaal*Math.pow((1+intrest),aantal_jaar);
    }
    HTML
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Interestformulier</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    /* <![CDATA[ */
    function berekening(){
    	var beginkapitaal=document.getElementById("beginkapitaal").value;
    	var intrest=document.getElementById("intrest").value;
    	var aantal_jaar=document.getElementById("aantal_jaar").value;
    	var eindkapitaal=document.getElementById("eindkapitaal").value;
    
    	eindkapitaal=beginkapitaal*Math.pow((1+intrest),aantal_jaar);
    }
    /* ]]> */
    </script>
    </head>
    <body>
    <form action="">
    
    	<table border="1" cellpadding="5">
    		<thead>
    			<tr>
    				<th>Beginkapitaal</th>
    				<th>Intrest</th>
    				<th>Aantal jaar</th>
    				<th>Eindkapitaal</th>
    			</tr>
    		</thead>		
    		<tbody>
    			<tr>
    				<td><input type="text" id="beginkapitaal" name="beginkapitaal" /></td>
    				<td><input type="text" id="intrest" name="intrest" onblur="if(document.getElementById('intrest').value>=1) document.getElementById('intrest').value=document.getElementById('intrest').value/100;"/></td>
    				<td><input type="text" id="aantal_jaar" name="aantal_jaar" /></td>
    				<td><input type="text" id="eindkapitaal" name="eindkapitaal" onselect="blur()" /></td>
    			</tr>
    		</tbody>
    	</table>
    	
    	<p>
    		<input type="button" value="Bereken eindkapitaal" onclick="berekening()" />
    		<input type="reset" value="Opnieuw beginnen" />
    	</p>
    	
    </form>
    
    </body>
    </html>
    The goal is to display the value outputted by the function berekening() in the input field with the id="eindkapitaal".
    Although the script does put out a value I do not succeed in getting it to show up in the textfield. Any suggestions/idea's why this is?

  2. #2
    Join Date
    Mar 2007
    Posts
    946
    Change your function to this

    HTML Code:
    function berekening(){
    	var beginkapitaal=document.getElementById("beginkapitaal").value;
    	var intrest=document.getElementById("intrest").value;
    	var aantal_jaar=document.getElementById("aantal_jaar").value;
    	document.getElementById("eindkapitaal").value = beginkapitaal*Math.pow((1+intrest),aantal_jaar);
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    65
    It works but why does this differ from assigning that line to a variable first and afterwards calling on the variable to put a value in it?

    thank you!

  4. #4
    Join Date
    Mar 2007
    Posts
    946
    If you wanted to call it later you could have done

    HTML Code:
    var eindkapitaal=document.getElementById("eindkapitaal");	
    eindkapitaal.value = beginkapitaal*Math.pow((1+intrest),aantal_jaar);

  5. #5
    Join Date
    Nov 2009
    Posts
    65
    I know but for some reason I thought you could store an attribute in a variable...whereas you can only store the value of that attribute and not the attribute itself.

  6. #6
    Join Date
    Mar 2007
    Posts
    946
    Quote Originally Posted by ajajajak View Post
    I know but for some reason I thought you could store an attribute in a variable...whereas you can only store the value of that attribute and not the attribute itself.
    Storing it like

    HTML Code:
    var eindkapitaal=document.getElementById("eindkapitaal");
    stores the object in eindkapitaal and then you are able to access its properties and methods.

    If you use your example

    HTML Code:
    var eindkapitaal=document.getElementById("eindkapitaal").value;
    it means you are storing the actual value of it. You can look at it as calling a function that returns a result and storing it in your variable. You can't call that function through your variable again.

  7. #7
    Join Date
    Nov 2009
    Posts
    65
    Ok that makes sense.

    I suspected it was something like that. Thank you for clarifying!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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