79577

Javascript simulate pressing enter in input box

I have what seemed to be a simple fix to a problem but after trying a ton of different solutions online nothing seems to work for me.

I have search field that searches a data table every time you type or press enter in the input field, however after I assigned a value to the search box when the page is loaded it wont do the search until after the user either presses enter in the box or types / deletes a letter. I was just trying to find a solution to simulate hitting enter but it didn't seem to work.

Here is my code:

<script type="text/javascript"> window.onload = (function() { document.getElementById('search').value = " <?php echo $search;?>"; }); </script>

This obviously assigns the value to what my php variable is set to which works fine the text is loaded in the box on page load, it just doesnt perform the search like I said unless the user presses enter in the box OR deletes/adds a letter in the input box.

The search box code is

<input type="text" id="search" aria-controls="DataTables_Table_0" class="text">

Update: This is documentation I found on exactly what I am using http://datatables.net/ref

Answer1:

Try this

document.getElementById("formId").submit();

Answer2:

You can call the onchange() event in javascript:

<script type="text/javascript"> window.onload = (function() { document.getElementById('search').value = " <?php echo $search;?>"; document.getElementById('search').onchange(); }); </script>

This answer also has an alternative: https://stackoverflow.com/a/3629423/890726

Answer3:

I assume you have a form, the enter in the textbox will trigger the submit of the form. In javascript you can do this manually like this:

<form id='myform" action="/foo"> document.forms["myform"].submit();

Answer4:

<input type="text" id="fname" onblur="businessFun()">

Please try this and in js buinessFun() do the search actions if you use ajax pass current time as parameter

Recommend

  • Delphi 7 - How to delete an item from listview using its caption
  • How to determine differences in two lists of data
  • Can't spy on an event handler even though I can spy on other methods
  • Use SendInput to lock the computer
  • Display a numeric keypad on activity without an input area
  • Delphi XE6 - How to make user defined components image show up on the pallette
  • Windows Phone 7.1 “Send To” functionality?
  • Change a string in xml into an array in xml easily
  • Cannot debug project after upgrading to Service Fabric SDK v2.3.301
  • CouchDB views erroring out
  • IE readonly textarea problem
  • How to set infinite shared access signature policy in azure?
  • How to make a user wait with Laravel
  • Reloading table causes flickering
  • Send multimedia commands
  • Mockery and Laravel constructor injection
  • C#: Import/Export Settings into/from a File
  • Cast between interfaces whose interface signatures are same
  • C++ Partial template specialization - design simplification
  • How to install a .deb file on a jailbroken iphone programmatically?
  • DomPDF {PAGE_NUM} not on first page
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • Importing jscolor library in angular 2
  • Why winpcap requires both .lib and .dll to run?
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • A cron job substitute?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal