Search code examples
javascripthtmlhref

getting value from input field and open a new link


im completely new to html && javascript coding so now im doing my first steps and trying to write a search bar which will pass google your input text

<script src="func.js"></script>
  <form name="googleSeach">
   <p align="center"><input name="searchTxt" id="searchTxt" type=search placeholder="Google Search">
   <input type="submit" value="Find" onclick="test()"></p>
  </form>

and javascript

function test(){
    window.open("https://www.google.ru/webhp?newwindow=#q="+document.getElementById('searchTxt').value,"_self")
}

well, thought it should work but no, it does not. What's the proble?


Solution

  • Believe you just want to use

    <form name="googleSeach" onsubmit="test(event)">
    

    Instead of onclick.

    Also your handler should probably cancel the submit action. Also use location.href instead of window.open since you're trying to open the new URL in the same window.

    http://jsfiddle.net/bvaughn/fr35mpgf/2/