0

I'm working on a page that should redirect depending on what the user enters in the form, and I can't seem to figure it out. New to Javascript, and eager to learn!

Here is the html:

<body>
<div id="header">
    <div id="navcontainer">
        <ul>
            <li> <a href="#item1">Home</a> </li>
            <li> <a href="#item2">Scents</a> </li>
            <li> <a href="#item3">About</a> </li>
            <li> <a href="#item4">Contact</a> </li>
        </ul>
    </div>  
</div>
<div id="content">
        <p>Hi, how are you feeling today?</p>
        <form> <input id="feels" type="text" name="feeling" size="35" onchange="checkfeels(#feels.value);"> 
        <input type="submit" value="submit" >
        </form>
</div>

And the Javascript I have so far:

function checkfeels(myFeels) {
        switch (myFeels) 
        {
        case "good":
            document.location.href = 'scents.html';
            break
        case "bad":
            alert(2);
            document.location.href = 'scents2.html';
        default: 
            alert("whatever");
        }
    }

Any help is really appreciated, thanks!

2
  • What is supposed to happen? What actually happens? What browser(s) did you try? Have you enabled debugging in your browser? If yes, did it report any errors? Commented Mar 31, 2013 at 21:24
  • I just want the user to answer the question, "How are you feeling?", and when they enter their answer, they should be directed to a new page depending on their answer. I've tried in Firefox and Chrome, and I didn't think to enable debugging..I'll look into that. Commented Mar 31, 2013 at 21:27

3 Answers 3

2

Put the

return checkfeels(this.elements.feeling.value)

in the form onsubmit attribute.

"feeling" in current case is the name of input field. <input type="text" name="feeling" > in your code.

And add

return false;

to the end of the checkfeels function to prevent default action (submiting form to server).

Also, you can pass to function whole form, for example:

return checkfeels(this)

And work already with whole form in checkfeels function.

function checkfeels(form) {
   var feeling = form.elements.feeling.value;
   var otherField = form.elements.otherField.value;

   // code with your logic here

   return false; // prevent default action
}
Sign up to request clarification or add additional context in comments.

Comments

1

Put the following code:

checkfeels($('#feels').val());
return false;

in the form onsubmit attribute.

1 Comment

When I do that, I'll just be taken to index.html?feeling=good rather than a new page. Am I doing something wrong..or do I just not understand how forms work?
0

You cannot get what the user types with #feels.value (it seems that you make a mistake between jQuery syntax and javascript).

You can do that in pure javascript :

function checkfeels() {
    var myFeels = document.getElementById('feels').value;
    switch (myFeels) {
        case "good":
            document.location.href = 'scents.html';
            break
        case "bad":
            alert(2);
            document.location.href = 'scents2.html';
        default: 
            alert("whatever");
    }
}

You get what the user type with :

document.getElementById('feels').value
and you don't need to give any parameters to the function.

If you use jQuery, you can get the value with :

$('#feels').val();

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.