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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Location
    Melbourne
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting a simple error in simple code.

    Hi, im currently using the following code to swap text between text boxes. I'm suppose to enter only numeric values, and its suppose to swap text only if numeric value in first text box is greater than second one. however in my code, some times it works, some times it doesnt. sometimes it even swaps even if its smaller than second value.



    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <!-- Minus AutoDato -->
    <TITLE>Sort Text</TITLE>
    <META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
    <script>
    function swap()
    {
    var form=document.switchtext;
    var t1=form.text1;
    var t2=form.text2;
    if (form.text1.value > form.text2.value) {
    var temp = t1.value;
    t1.value = t2.value;
    t2.value=temp;
    }
    }
    </script>

    </HEAD>
    <BODY>

    <b><i> Please insert only numeric values. </i><b>
    <form name="switchtext">
    <input type="text" name="text1"/> <br />
    <input type="text" name="text2"/> <br />
    <input type="button" name="switch" value="Switch Text" onclick=swap()>


    </BODY>
    </HTML>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <!-- Minus AutoDato -->
    <TITLE>Sort Text</TITLE>
    <META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
    <script>
    function swap()
    {
    var t1=document.switchtext.text1.value;
    var t2=document.switchtext.text2.value;
    if (t1 < t2) {
    var temp = t1;
    switchtext.text1.value = t2;
    switchtext.text2.value = temp;
    }
    }
    </script>

    </HEAD>
    <BODY>

    <b><i> Please insert only numeric values. </i><b>
    <form name="switchtext">
    <input type="text" name="text1"> <br>
    <input type="text" name="text2"> <br >
    <input type="button" name="switch" value="SwitchText" onclick="swap()">


    </BODY>
    </HTML>

    You will need to include a check to ensure that only numeric values are entered by the user, otherwise the script will throw an error.


  •  

    Posting Permissions

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