-2

I have a custom search bar but I don't know how to get it to function with a js code.

This is my Search Button:

script type = "text/javascript" >
  document.getElementById('searchform').onsubmit = function() {
    window.location = 'http://www.google.com/search?q=site:yourdomainname ' + document.getElementById('test').value;
    return false;
  }
<!DOCTYPE html>
<center>
  <html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
      input[type=text] {
        width: 130px;
        box-sizing: border-box;
        border: 2px solid #000000;
        border-radius: 4px;
        font-size: 16px;
        background-color: white;
        background-image: url('searchicon.png');
        background-position: 10px 10px;
        background-repeat: no-repeat;
        padding: 12px 20px 12px 40px;
        -webkit-transition: width 0.4s ease-in-out;
        transition: width 0.4s ease-in-out;
      }
      
      input[type=text]:focus {
        width: 100%;
      }
    </style>
  </head>

  <body>

    <form>
      <input type="text" name="search" placeholder="Search..">
    </form>

  </body>

  </html>

If you can help me that would be great, thank you.

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Adrian
  • 63
  • 8
  • 3
    What version of Java is this? I don't think I've seen it before – Snow May 13 '19 at 09:31
  • I got it from this post here: https://stackoverflow.com/questions/5207309/how-do-i-get-my-search-bar-to-actually-work look at the answer that was posted below and you'll see it. I'm trying to use the same method but different button. – Adrian May 13 '19 at 09:35

1 Answers1

0

There is no element searchform nor test declared in your html that you could getElementById.

Change your form to something like this:

<form id="searchform">
  <input id="test" type="text" name="search" placeholder="Search..">
</form>
Selaron
  • 6,105
  • 4
  • 31
  • 39