1

I am brand new to Javascript and am just using it to make a simple website for fun. I have tried searching the web but am still stuck, so if you could help me or redirect me towards other help, that would be great. I am trying to use Javascript to send a user to another html page in my site if their input matches my criteria. So I wanted to use an if/else statement to do this: if the text input equals ODQHVHMJKD, it would send them to page3.html. However when I try this on the browser, nothing happens--it just takes me to an identical page with ?codebox1=f&button1=Submit at the end of the address. Here is my script section:

<script type="text/javascript">
function testResults (form) {
    if (form.codebox1.value == ODQHVHMJKD) {
    window.location.pathname = 'page3.html';
    }
    else {
        window.alert("Try again!");
    }  
};
</script>

Here are my form elements:

<form name="form1" method="GET">
<input name="codebox1" type="text" />
<input name="button1" type="submit" value="Submit" onClick="testResults(this.form)"/>
</form>

Can you help me so that I can get this to work? It's more than likely I've done everything completely wrong--any help is appreciated!

2
  • So I added return false and quotes. The alert box works but when I type the correct answer it still doesn't take me to the new page. Any suggestions? I've made sure I'm using the right file name. Commented Jun 6, 2014 at 3:04
  • I got it all to work! I was using window.open incorrectly. I changed it to window.open("page3.html"); and now everything seems to be just fine! Commented Jun 6, 2014 at 17:14

3 Answers 3

1

Try this,

function testResults (form) {
    if (form.codebox1.value == "ODQHVHMJKD") {
        window.location = 'page3.html';
    }
    else {
        window.alert("Try again!");
    }  
    return false;
};
Sign up to request clarification or add additional context in comments.

1 Comment

This works in that the alert box now shows up when I type in the wrong answer, but I'm still having trouble linking to the new page. I've triple checked that I'm using the right name, so I don't know why it's not working.
1

You need to prevent the default action of the form. In the submit event, call e.preventDefault(); or return false In addition, you need quotation marks around ODQHVHMJKD

Js Fiddle: http://jsfiddle.net/prankol57/Ht45t/

Comments

0

Maybe this can help you.

    <form name="form" onsubmit="Results()">
    <input type="text" name="fname" id="val">
    <input type="submit" value="Submit">
    </form>

    <script type="text/javascript">
        function Results() {
          var val = document.getElementById('val').value;
          if (val == "ODQHVHMJKD") {
              window.location = 'page3.html';
           } else {
              window.alert("Try again!");
           }  
        };
    </script>

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.