www.webdeveloper.com
Results 1 to 6 of 6

Thread: form variables

  1. #1
    Join Date
    Nov 2006
    Posts
    4

    form variables

    sorry if this seems a very stupid question,

    How do I get text form input to be a variable? I am tring to write a program that will add a number to a number a certain number of times and I want the user to input that data in 3 text forms above it. But i cannot get the form input to work.
    I am also tring to get the program to run with a submit button click. The loop is just a very simple for loop.

    function test() {
    for(i=1;i<num;i++) {
    number += number + numadd;
    }
    }

    and the submit code was

    <input type="submit" value="subbutton" onclick="test()">

    thats not all the code... but i think thats where the errors would be.
    But could someone still tell me how to turn form text input into a variable...

    Thanx

  2. #2
    Join Date
    Nov 2002
    Posts
    4,473
    number += number + numadd;
    This would cause number to increase by (number + numadd). Is that really what you wanted to do?

    Form fields are strings. Javascript seee "+" as concatenate if you use it in conjunction with strings. To make it do math, you have to tell javascript that the value is a number. You can do this using several methods, such as parseInt() or by using multiplication to "fool" it.
    Code:
    number = (document.formname.textboxname.value * 1) + 32;

  3. #3
    Join Date
    Nov 2006
    Posts
    4
    Code:
    <html>
    	<head>
    		
    		<title>Javascript Program</title>
    	<script language="JavaScript">
    	function test() {
    		for(i=1;i<num;i++) {
    			document.write(numb);
    			numb += numb + numadd;
    			
    		}
    	}
    	</script>
    	</head>
    	<body>
    	<form name="numinput">
    	<INPUT TYPE="text" id="num"><br>
    	<INPUT TYPE="text" id="numadd"><br>
    	<INPUT TYPE="text" id="numb"><br>
    	<INPUT TYPE="submit" VALUE="Do it!" onclick="test()">
    	<script language="JavaScript">
    	num = (document.numinput.num.value * 1);
    	numadd = (document.numinput.numadd.value * 1);
    	numb = (document.numinput.numb.value * 1);
    	</script>
    	</body>
    </html>
    its not really working can someone please tell me what wrong?

  4. #4
    Join Date
    Nov 2006
    Posts
    526
    The problem is that you are setting the values before the user has entered anything.

    Now I'm not so sure that the code is doing what you really want it to as it doesn;t make sense to me, but this will get you going:
    Code:
    <html>
    <head>
    <title>Javascript Program</title>
    
    <script language="JavaScript">
        function test(formObj) {
            num = (formObj.num.value * 1);
            numadd = (formObj.numadd.value * 1);
            numb = (formObj.numb.value * 1);
    		for(i=1;i<num;i++) {
    			document.write(numb);
    			numb += numb + numadd;
    			
    		}
    	}
    	</script>
    	</head>
    	<body>
    	<form name="numinput">
    	<INPUT TYPE="text" id="num"><br>
    	<INPUT TYPE="text" id="numadd"><br>
    	<INPUT TYPE="text" id="numb"><br>
    	<INPUT TYPE="submit" VALUE="Do it!" onclick="test(this.form)">
    	<script language="JavaScript">
    
    	</script>
    	</body>
    </html>

  5. #5
    Join Date
    Nov 2006
    Posts
    4
    im trying to get the user to enter a number then have a number added to that number a certain number of times...

  6. #6
    Join Date
    Nov 2006
    Posts
    526
    Then the code should go something like this:

    Code:
        function test(formObj) {
            num = (formObj.num.value * 1);
            numadd = (formObj.numadd.value * 1);
            numb = (formObj.numb.value * 1);
            for(i=0;i<num;i++) {
                document.write(numb);
                numb += numadd;
            }
        }

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