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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post

    basic math, document.write and input fields

    hi. i have forgotten what little i once knew of js. trying to write a function that sets 2 variables which are the product of 2 text input form fields, and performs a single simple formula either onlostfocus or onmouseout or onclick of something (anything), then displays the answer either via an alert box or just document.write

    solely for clarification, here's a hamfisted psuedo-script:

    Code:
    <html>
    <head>
    <script>
    var a = form1.field1.value;
    var b = form1.field2.value;
    function calc(); {
    document.write = ('(((((a*a)*a)/((b+1)-a))*a)+(100/b))/2)');
    }
    </script>
    </head>
    <body>
    <form id="form1" name="form1" method="getblahblah">
    <input type="text" name="field1" size="2" id="field1" value="1" onlostfocus="calc()";>
    <input type="text" name="field2" size="2" id="field1" value="1"
    onlostfocus="calc()";>>
    </form>
    </body>
    </html>
    Last edited by subhailc; 07-07-2004 at 01:05 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    <html>
    	<head>
    		<script type="text/javascript">
    			function calc() {
    				var a = document.form1.field1.value;
    				var b = document.form1.field2.value;
    
    				document.write((a * a * a * a / ((b + 1) - a) + 100 / b) / 2);
    			}
    		</script>
    	</head>
    	<body>
    		<form name="form1" method="getblahblah">
    			<input type="text" name="field1" size="2" value="1" onBlur="calc()";>
    			<input type="text" name="field2" size="2" value="1" onBlur="calc()";>>
    		</form>
    	</body>
    </html>

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes. What he said^^. You're getting the variable values before the user sets them or that part of the html is even created.

  • #4
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    thank you


  •  

    Posting Permissions

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