SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Referencing the textbox's data

    When I add in for the part in red m=1 it prints out "1" in the textbox so the error must be in the part in red.

    <script type="text/javascript">
    function monthdate(){
    var m=number(window.document.myform.mo.value);
    window.document.myform.t.value = m;
    }
    </script>
    <form name="myform">
    <table><tr><td align="Center"><INPUT TYPE="text" NAME="mo" VALUE="9" SIZE="4"></td><INPUT TYPE="button" NAME="month" VALUE="Calculate month" onClick="monthdate()"/></td><td align="Center" colspan="4"><INPUT TYPE="text" NAME="t" SIZE = ""></td></tr></table>
    </form>

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please ignore the form there are errors.

  3. #3
    SitePoint Addict AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will do it, but you need error handling for out of range numbers and non-number entries. I have removed your number object and replaced it with a parseInt() which does the same job. The short cut to the form just simplifies the code making it more readable. I also needed to add a little to your table.

    <html>

    <head>

    <script type="text/javascript">
    <!--
    // initialise on page load
    var form1; // global
    function shortCut()
    { form1=window.document.myform;
    }
    // -------------
    // you need error handling here to cover alpha characters
    // and numbers >12 and <1
    //
    function monthdate()
    { form1.result.value = parseInt(form1.mo.value);
    }
    //-->
    </script>
    </head>

    <body onload="shortCut()">

    <form name="myform">
    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse" width="300">
    <tr>
    <td><input type="text" name="mo" value="9" SIZE="4"></td>
    <td>
    <input type="button" name="month" value="Calculate month" onClick="monthdate()"></td>
    <td><input type="text" name="result" size="5"></td>
    </tr>
    </table>
    </form>

    </body>

    </html>


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
  •