0
   <class="searchform">
<input type="text" id="textbox" placeholder="Search..." checkbox_test(this.value);">
<input type="submit" id="button" class="classname" onClick="javascript:checkbox_test();">Search</a>

I have a custom search engine with checkbox options. I have a script called "checkbox_test" that will grab the text from the textbox and run the search query plus additional options selected with checkboxes.

The problem I have is that when I press 'Enter' nothing happens, I have tried many examples from stackoverflow and examples I found on the internet but it does not seem to work for me. Any ideas how to solve this problem?

Basically I want to run the javascript when the visitor presses enter (with and without focus)

  • What does checkbox_test do when it receives, and doesn't receive a parameter? – EWit Jul 22 '14 at 14:11
  • if no boxes are checked it gives an alert that you need to specify what you are searching for. if boxes are checked it adds a string to the url (e.g. &filetype=.doc) – Bret van Putten Jul 22 '14 at 14:19

2 Answers2

0

HTML:

<body onkeypress="wasEnter(event)">....

Javascript:

function wasEnter(e) {
    if (e.keyCode == 13) {
        // here you can do your stuff
    }
}
Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
0

You've got a markup error there. That's probably why nothing you try is happening.

Try the following:

<input type="text" id="textbox" placeholder="Search..." onkeypress="checkbox_test(this.value);">
brainbowler
  • 667
  • 5
  • 17
  • Thanks for your quick response, This works and submits the textbox but also when typing your search query. I need the javascript to start running only when the Enter button is pressed – Bret van Putten Jul 22 '14 at 14:16
  • Ok. I get what you actually want. Have a look at that one then: http://stackoverflow.com/questions/13987300/how-to-trigger-enter-key-press-of-textbox – brainbowler Jul 22 '14 at 14:21
  • Thanks a lot I finally got it working! For future references the fix was: onKeydown="Javascript: if (event.keyCode==13) nameofjavascript(); – Bret van Putten Jul 22 '14 at 14:32