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 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2003
    Location
    Belgium
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getting a value into a textbox

    How can I get the value of "c" into the textbox result?
    Can it be done with this code? If yes, what needs to be changed?
    If no, it has to


    Code:
    <head>
      <script language="JavaScript">
        function getC(a,b,c) {
          c = a * b
          return c }
      </script>
    </head>
    
    <body>
    <form>
      <input type="textbox" name="val1">
      <input type="textbox" name="val2">
      <input type="button" value="Calculate" onClick="getC(document.forms[0]val1.value,document.forms[0]val2.value,document.forms[0].val3.value)>
      <input type="textbox" name="val3">
    </form>
    </body>
    SToFFeL

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Your function is close, but I re-wrote quite a bit just because of how I typically program. Not sure if it is really any different.

    <html>
    <head>
    <script language="JavaScript">
    function getC(myform) {
    myform.val3.value = myform.val1.value * myform.val2.value;
    }
    </script>
    </head>

    <body>
    <form>
    <input type="text" name="val1">
    <input type="text" name="val2">
    <input type="button" value="Calculate" onClick="getC(this.form)">
    <input type="text" name="val3">
    </form>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Jan 2003
    Location
    Belgium
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can it also be done without the
    myform.val3.value = myform.val1.value * myform.val2.value;
    ? So it would be something like this: c = a * b
    SToFFeL

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    <html> 
    <head> 
    <script language="JavaScript"> 
    function getC(a,b) {
          	c = a * b;
    	return c; 
    }
    </script> 
    </head> 
    <body> 
    <form> 
    <input type="text" name="val1"> 
    <input type="text" name="val2"> 
    <input type="button" value="Calculate" onClick="this.form.val3.value=getC(this.form.val1,this.form.val2)"> 
    <input type="text" name="val3"> 
    </form> 
    </body> 
    </html>

  • #5
    New Coder
    Join Date
    Jan 2003
    Location
    Belgium
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmz, I get an NaN error,
    (I'll fix it)
    Thnx
    SToFFeL

  • #6
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Just add .value to the end of both parameters when it is called in the onClick.

    onClick="this.form.val3.value=getC(this.form.val1.value,this.form.val2.value)"

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    oh yes, i forgot that...
    you may also want to check if the input is a number or not:

    function getC(a,b) {
    if (isNaN(a) || isNaN(b)){
    alert("Please enter a valid number.");
    return;
    }
    c = a * b;
    return c;
    }

  • #8
    New Coder
    Join Date
    Jan 2003
    Location
    Belgium
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thnx!!
    SToFFeL


  •  

    Posting Permissions

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