user2017769
user2017769

Reputation: 13

Launch a google search using an input box

The problem I have is: I have an inputbox and a submit button with which I would like to launch a google search query.

There are two ways I want the search query to be launched: 1. By pressing the submit button 2. By pressing enter in the inputbox

Now I can do this in a form, but the thing is I can't use a form because it screws with my webpage in Internet Explorer.

So far I've managed to get 1. (pressing the submit button) to work. But I can't figure out to get 2. (pressing enter) to work. Also I need the google search to open in a new window.

<input id="textbox" type="text" placeholder="Search on Google...">
<a id="googleLink" href="notrequired" onclick="this.href='http://www.google.com/search?q=' + encodeURIComponent(document.getElementById('textbox').value);">
    <span>Search</span>
</a>

Thanks in advance!

Upvotes: 1

Views: 5883

Answers (1)

v2b
v2b

Reputation: 1446

the following should work,

Add a keypress handler on the input and check for event 13 (enter), and write the re-direction logic in there.

<input id="textbox" type="text" placeholder="Search on Google..." onkeydown="if (event.keyCode == 13 || event.which == 13) { location='http://www.google.com/search?q=' + encodeURIComponent(document.getElementById('textbox').value);}" />

Upvotes: 2

Related Questions