0

So, I have a form on my HTML page, and a separate button that links to the form. My question is: when the user enters a certain input (in this case a string) and clicks on the button, how do I connect the user to a different HTML page based on the corresponding user input from the form? I know I have to use javascript, but any specific code will be extremely helpful. Thanks!

ADDDED MY CODE:

<!doctype html>
<html>
<head>
    <title>Home</title>
    </head>

    <body>
            <form id = "user-info" onSubmit="myFunction()">
                <div class = "favorite-fruit">
                    <p>Enter your favorite fruit<br></p>
                        <input type="text" name="fav-fruit" id = "fruit"><br>
                </div>
                <div class="favorite-vegetable">
                    <p>Enter your favorite vegetable<br></p>
                        <input type="text" name="fav-vegetable" id="vegetable">
                </div>
            </form>
            <a class = "confirm" onSubmit="myFunction()">
                <button form= "user-info" type="button" name= "confirm-input">Confirm</button>
            </a>
        </div>


        <script type="text/javascript">
            function myFunction(){
                var firstFruit = document.getElementById("fruit").innerHTML;
                var secondVegetable = document.getElementById("vegetable").innerHTML;
                var f = firstFruit;
                var s = secondVegetable;

                if(f.value == "Apple" && s.value == "Broccoli"){
                    //GO TO "appleBroccoli.html"
                }
                else if(f.value == "Grapes" && s.value == "Carrots"){
                    //GO TO "grapesCarrots.html"
                }
                else if(f.value == "Strawberry" && s.value == "Kale"){
                    //GO TO "strawberryKale.html"
                }



            }



        </script>
      </body>
</html>
3
  • 1
    can you please update your code here. Commented Dec 11, 2018 at 5:01
  • 1
    You really should show us some effort in what you've done. You can read more about creating a Minimal, Complete, and Verifiable example. Commented Dec 11, 2018 at 5:05
  • window.location.href, window.open, form submission Commented Dec 11, 2018 at 14:25

2 Answers 2

1

It can be done by taking up the value of input tag like :-

var value = $(element).val();

Then resetting the action attribute of the form tag with the new value like :-

$(formElem).attr("action",value+".html")

I guess this would help u..

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

2 Comments

He/she seems to be using plain vanilla JS so it'd be better to mention that your suggestion is based on jQuery ;-) Also, this: value+".html" would end up in Apple.html or Broccoli.html which doesn't seem to be the desired outcome because he wants a fixed/common page for multiple choices. You'd also need some checks in place to avoid things like fudfud.html or 0123.html from happening
ya sure @FrancescoG. the time I answered the question , the code wasn't mentioned. There after , I explained assuming he/she should get a basic gist of my solution . :) But anyways , thanks for correcting me . Will definitely keep such things in mind for future commenting .. :)
1

Your form, user-info doesnt have a submit button

add <input type = 'submit' name='conform'>Confirm</input> inside the form

Set action and method to the form to the corrosponding target HTML page.

<form action='..target html' method ='get'>
</form>

If the input is brocolli, you can change the form action as

document.getElementById('user-info').action = <new target>

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.