0

I'm using the following code and it works fine by clicking on the search button. But how can I also search using the enter key?

Code:

<script>
function mySearch() {
  var text = document.getElementById('strSearchText').value;
  var url = "privatelink=" + text;

  window.open(url,'_blank');
}
</script>
<style>
button:hover {color:red;}
.rounded-corners {
    -moz-border-radius: 20px;
    -webkit-border-radius: 20px;
    -khtml-border-radius: 20px;
    border-radius: 20px;
    white-space:normal;
    font-weight:bold;
    width: 50%;
}
.smallbutton {
    -moz-border-radius: 20px;
    -webkit-border-radius: 20px;
    -khtml-border-radius: 20px;
    border-radius: 20px;
    white-space:normal;
    font-weight:bold;
    width: 20%;
}
</style>
<br>
<input type="text" size="25" tabindex="1" value="" id="strSearchText">&nbsp;
<button class="smallbutton" onclick="mySearch();"><span style="DISPLAY: inline-block">Search</span></button>
Volker247
  • 1
  • 3

1 Answers1

0

Stick it in a form, and it does that by default

<form id="myform">
    <input type="text" size="25" tabindex="1" value="" id="strSearchText">
    <button class="smallbutton">
        <span style="display: inline-block">Search</span>
    </button>
</form>

and then

var form = document.getElementById('myform')

form.addEventListener('submit', function() {
  var text = document.getElementById('strSearchText').value;
  var url  = "privatelink=" + text;

  window.open(url,'_blank');
}, false);
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • I have another question if you don't mind. Is it possible searching from three different links? like under "Var URL = 3 links"? – Volker247 Apr 11 '15 at 16:08
  • Not while redirecting, you would have to write custom serverside scripts that scrape those URL's and return the contents etc. – adeneo Apr 11 '15 at 17:10
  • Thanks very much Adeneo. Will look into that soon. – Volker247 Apr 13 '15 at 10:17