2

Wondering how to simulate an enter key press into a javascript function. I cannot use Jquery.. Must be javascript. Here is some code:

<script type="text/javascript">
 function SetInput(string) {
console.log(string);
searchbox = document.getElementById("searchbox");
searchbox.value = string;

 } 
 console.log("fired!")
</script>
  <input class="search" placeholder="Search"  id="searchbox" />
DirectXgameR
  • 121
  • 1
  • 8
  • 3
    Usually keypresses and other user input will call functions. So simulating that, is really just calling the function directly. – azium Sep 21 '15 at 23:29
  • http://stackoverflow.com/q/905222/5247200 – David Sep 21 '15 at 23:30
  • If this is in a form, you can submit the form. – jfriend00 Sep 21 '15 at 23:31
  • Unfortunately, not a form. this is a search box which makes this confusing. – DirectXgameR Sep 21 '15 at 23:33
  • What exactly do you mean by "simulate an enter key press into a javascript function"? – Maximillian Laumeister Sep 21 '15 at 23:38
  • Since this isnt a form, i need a way to take the text that gets entered into the textbox, and "submit" it without user intervention. – DirectXgameR Sep 21 '15 at 23:42
  • We are attempting to use a tag cloud. this should allow people to press tags and have them entered into the search box. this works. however the search requires the enter button to be pressed to start searching and we need to simulate the pressing of the enter key. edit: yes DirectXgameR and I are affiliated on this project – JL Griffin Sep 21 '15 at 23:44
  • JavaScript is event driven, meaning some event has to occur before you can make things happen. The user must push a key or click somewhere on the page before you can relaly do anything with an event. Seeing how a keypress is an event, you cannot do it without user interaction. You can simlulate the enter key if the user presses another key, or simulate a line return without the user having to hit the enter key. – Adam Buchanan Smith Sep 21 '15 at 23:52
  • I'm guessing you want something like this: http://jsfiddle.net/cujm21wn/1/ – Jared Farrish Sep 21 '15 at 23:54
  • Here is what we are trying to do. I have a page built here: http://jargon-cool.codio.io:3000/ There is a drop down. That drop down, when a element is selected, types text into the text box. That text does nothing until i hit enter. – DirectXgameR Sep 21 '15 at 23:57
  • 1
    Wrap the functionality that "Enter" triggers into it's own function (as I demonstrate in the fiddle above), and when your `select` element `onchange` changes, push that text to the `input` and call the function that "Enter" calls on it's event. – Jared Farrish Sep 21 '15 at 23:59

0 Answers0