Angus Dobson
Angus Dobson

Reputation: 41

How to get search bar to take input and go to page

I'm trying to get a search box that when it takes an input it sends the user to a specific page. But for some reason that, I cannot figure out, it doesn't do anything when an input is given. My code is the following:

<!DOCTYPE html>
<html>
    <head>
        <title>TEST</title>
        <script type="text/javascript">
            var input = document.getElementById("search");

            function sendToPage(){
                if (input.value == "happy"){
                    location.href="suggestion_happy.html";
                }
                else if (input.value == "sad"){
                    location.href="suggestion_sad.html";
                }
                else {
                    alert('Invalid Input.');
                }
                    }
        </script>
    </head>
    <body>
        <div>
            <form onsubmit="sendToPage()">
                <input type="text" method="put" id="search" placeholder="Search" value="">
            </form>
        </div>
    </body>
</html>

Please help I'm still kind of new to javascript :)

Upvotes: 1

Views: 8034

Answers (3)

hassan
hassan

Reputation: 8278

you don't need to create a form to do this

check out this code

<!DOCTYPE html>
<html>
    <head>
        <title>TEST</title>
        <script type="text/javascript">


            function sendToPage(){
                var input = document.getElementById("search").value;
                 //alert(input);
                if (input == "happy"){
                    location.href = "suggestion_happy.html";
                    return false;
                }
                else if (input == "sad"){
                    location.href = "suggestion_sad.html";
                    return false;
                }
                else {
                    alert('Invalid Input.');
                }
                    }
        </script>
    </head>
    <body>
        <div>
                <input type="text" method="put" id="search" placeholder="Search" value="">
                <input type='submit' onclick="sendToPage();" />
        </div>
    </body>
</html>

Upvotes: 2

Shaymol Bapary
Shaymol Bapary

Reputation: 458

  <script type="text/javascript">


            function sendToPage(){
                var input = document.getElementById("search").value;
                 //alert(input);
                if (input == "happy"){
                    //location.href="suggestion_happy.html";                    
                    location.replace("suggestion_happy.html");
                    return false;
                }
                else if (input == "sad"){
                    location.replace("suggestion_sad.html");
                    return false;
                }
                else {
                    alert('Invalid Input.');
                }
                    }
        </script>

        <div>
            <form action="" onsubmit="return sendToPage()" method="post">
                <input type="text" id="search" placeholder="Search" value="">
            </form>
        </div>

I have edited my asnwer plz try this

Upvotes: 1

lucas
lucas

Reputation: 1505

there are two problems here:

  1. submitting a form will always refresh the page. So onsubmit is killing your app
  2. by putting the js in the <head> your document.getElementById call fails because that element doesn't exist yet. Javascript belongs at the end of the page, just before the closing </body> tag

Upvotes: 0

Related Questions