0

My goal is to have a text box and a button. If I enter "Hello" in the text box and press the submit button I would like to have see the text box filled with "World.

For the moment the value of the text box will be changed betweeen the

<html>
   <body>
         <script>
            function validateForm() {
                var x = document.forms["myForm"]["Input"].value;
                if (x == "Hello") {
                    alert("test");
                    document.getElementById("Input").value = "World";
                    alert("test2");
                }
            }
        </script>

        <form name="myForm" action="test.html" onsubmit="return validateForm()" method="post">
        Input: <input type="text" id="Input"><br>
        <input type="submit" value="Submit">
        </form>

   </body>
</html>
3
  • What is the problem with your code? Commented Aug 26, 2015 at 11:43
  • I tried the code locally (Firefox and Chrome) and the text box gets filles with "World" Commented Aug 26, 2015 at 11:46
  • I'm running it on my Windows 10 machine with chrome and the field is empty once i click away the second alert message. Could it be the extension of my html file is wrong? I'm saving my code in a .html file and just open it in my browser Commented Aug 26, 2015 at 11:51

3 Answers 3

1

The problem is that the form gets submitted right after the validation. So you are redirected to test.html again.

If you don't want that to happen, add event.preventDefault(); to your Event Handler (check out the fiddle to see it working):

<html>
   <body>
         <script>
            function validateForm(event) {
                event.preventDefault();

                var x = document.forms["myForm"]["Input"].value;
                if (x == "Hello") {
                    alert("test");
                    document.getElementById("Input").value = "World";
                    alert("test2");
                }
            }
        </script>

        <form name="myForm" action="test.html" onsubmit="return validateForm(event)" method="post">
        Input: <input type="text" id="Input"><br>
        <input type="submit" value="Submit">
        </form>

   </body>
</html>

You can learn more about event.preventDefault() at MDN.

Just as a sidenote: It is generally better to use addEventListener instead of the onsubmit attribute (Better separation of concerns, you can add multiple event listeners, etc.).

Sign up to request clarification or add additional context in comments.

Comments

0

When you submit your page, then the content in the action page will be loaded.

In your case test.html will be loaded.

If you want the value "World" to be shown in the text box on hitting the submit, then return false on your validateForm() method.

Comments

0

Use return false; to stay on the same page and stop form submission.

function validateForm() {
    var x = document.forms["myForm"]["Input"].value;
    if (x == "Hello") {
        document.getElementById("Input").value = "World";
        return false;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.