SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    greece
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript mess up?

    Hi i made this code but it doesn't work

    Code:
    <html>
    
    	<head>
    		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
    		<meta name="generator" content="Adobe GoLive">
    		<title>Untitled Page</title>
    	<script type="javascript"><!--
    function calc( )
    {
    var text1=document.form.input.value;
    var text2=document.form.field2.value;
    var text2a=12;
    var text3=document.form.field3.write.value=text1*text2a+text2;
    document.form.field3.write. (var text3);
    }
    </script>
    
    	</head>
    
    	<body bgcolor="#ffffff">
    		<script type="javascript" language="JScript5.0">
    function calc( );
    </script>
    
    		<p></p>
    		<form id="form" action="(EmptyReference!)" method="get" name="form">
    			Field1
    			<p><input type="text" name="field1" size="2"></p>
    			<p>Field2</p>
    			<p><input type="text" name="field2" value="1.00" readonly size="1"></p>
    			<p>Field3</p>
    			<p><input type="text" name="field3" size="4"></p>
    		</form>
    		<p></p>
    	</body>
    
    </html>
    anybody can give a hand?

    the calculation should be automatic and displayed in field3.
    Thanks

  2. #2
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    think what this is what you want to do?

    Code:
    <html>
    
    	<head>
    		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
    		<meta name="generator" content="Adobe GoLive">
    		<title>Untitled Page</title>
    	<script type="text/javascript">
    
        function calc()
        {
        var text1 = parseInt(document.form.field1.value);
        var text2 = parseInt(document.form.field2.value);
        var text2a = parseInt(12);
        var text3 = parseInt((text1*text2a)+text2);
        document.form.field3.value = text3;
        }
    
    </script>
    
    	</head>
    
    	<body bgcolor="#ffffff">
    
    		<p></p>
    		<form id="form" action="(EmptyReference!)" method="get" name="form">
    			Field1
    			<p><input type="text" name="field1" size="2" onchange="calc();"></p>
    			<p>Field2</p>
    			<p><input type="text" name="field2" value="1.00" readonly size="1"></p>
    			<p>Field3</p>
    			<p><input type="text" name="field3" size="4"></p>
    		</form>
    		<p></p>
    	</body>
    
    </html>
    the parseInt() function lets javascript know its adding numbers instead of strings, and the onchange="calc()" calls the function whenever field1 is changed so that field3 stays updated all the time

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    greece
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THanks man that's it. i'm grateful

  4. #4
    SitePoint Member
    Join Date
    Dec 2004
    Location
    greece
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    however, i noticed that if the user enters 0, the sum will be 1 . is there any way to call the function only if the number entered in field1 is higher than 0? So that whenthe user enters 0 the sum will be 0?

  5. #5
    SitePoint Member
    Join Date
    Dec 2004
    Location
    greece
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i also changed the onchange to onkeyup, and it works much better.

  6. #6
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try the following

    Code:
    <html>
    
    	<head>
    		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
    		<meta name="generator" content="Adobe GoLive">
    		<title>Untitled Page</title>
    	<script type="text/javascript">
    
        function calc()
        {
        var text1 = parseInt(document.form.field1.value);
        var text2 = parseInt(document.form.field2.value);
        var text2a = parseInt(12);
        var text3 = parseInt((text1*text2a)+text2);
    
          if((text1 < 1) || (text1 == 0))
          {
              document.form.field3.value = 0;
          }
          else
          {
          document.form.field3.value = text3;
          }
        }
    
    </script>
    
    	</head>
    
    	<body bgcolor="#ffffff">
    
    		<p></p>
    		<form id="form" action="(EmptyReference!)" method="get" name="form">
    			Field1
    			<p><input type="text" name="field1" size="2" onkeyup="calc();"></p>
    			<p>Field2</p>
    			<p><input type="text" name="field2" value="1.00" readonly size="1"></p>
    			<p>Field3</p>
    			<p><input type="text" name="field3" size="4"></p>
    		</form>
    		<p></p>
    	</body>
    
    </html>
    theres heaps of ways it can be done though

  7. #7
    SitePoint Member
    Join Date
    Dec 2004
    Location
    greece
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that will do. thank you very much. Since i got that i think i an make some more complex things...We'll see... Thanks anyway.


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
  •