0

So we have a simple code that searches a set link we have. On opening, the page use enters the search query and then clicks search/submit. We want it to be set to that when they hit ENTER it automatically searches rather than having to use the enter button. Needless to say it will NOT open google I've just used that to display here

it's not working despite many efforts can anyone help me here at all?

<html>
    <!-- search function for loop - widget code
     created: 23/9/20
     last update: 24/9/20
     v 2.0.9 - corrected link to open in a new tab
    -->
    <head>
        <title>Search Loop</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script</script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#button').click(function(e) {  
                    var inputvalue = $("#input").val();
                    window.open ("http://www.google.co.uk");
                });
            });
        </script> 
    </head>
    <body>
        <p> Search Loop now ...</p>       
        <input type="text" value="" id="input"> 
        <input type="button" id="button" value="Search" onlick="document.button.submit();">
    </body>
</html>
iAmOren
  • 2,760
  • 2
  • 11
  • 23
  • 4
    use a `
    ` with a `
    – cloned Sep 28 '20 at 12:29
  • you can also check this topic: https://stackoverflow.com/questions/6799533/how-to-submit-a-form-with-javascript-by-clicking-a-link – nima Sep 28 '20 at 12:53
  • In the first input (text), you can add `onchange="document.button.submit();"`. By the way, in the second input (button), you have a typo: `onlick` where `onclick` should be. – iAmOren Sep 28 '20 at 16:28
  • @Murtaza Ahmad, you should not fix bugs/typos in question: you can comment and point them out. – iAmOren Sep 28 '20 at 16:36
  • @iAmOren ahh oops thank you for that! not sure if that was from the original code or when I copied and pasted :) – Odriscoll87 Sep 29 '20 at 21:52

2 Answers2

0

Wrap your input elements with a form. You don’t need any JavaScript here:

<form action="http://www.google.co.uk" target="_blank">
  <input name="q" type="text" value="" id="input">
  <input type="button" id="button" value="Search">
</form>
Taufik Nurrohman
  • 3,329
  • 24
  • 39
0

If you wish you update your JavaScript, just add a function that will call button click function when you type something in the input box and hit enter.

    <script type="text/javascript">
        $(document).ready(function() {
            $('#button').click(function(e) {  
                var inputvalue = $("#input").val();
                window.open ("http://www.google.co.uk");
            });
            var input = document.getElementById("input");
            input.addEventListener("keyup", function(event) {
                if (event.keyCode === 13) {
                    event.preventDefault();
                    document.getElementById("button").click();
                }
            });

        });
    </script>
Akshay
  • 126
  • 6