2

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

1
  • Pressing enter in a text <input> typically triggers the submit event on its <form>. Commented Oct 12, 2012 at 19:46

4 Answers 4

1

Try this

document.getElementById("formId").submit();
Sign up to request clarification or add additional context in comments.

2 Comments

For some reason none of this is working. I found this documentation that shows exactly what I am using, perhaps this will clarify exactly what I should use datatables.net/ref
Works for me. Why don't you make us a JsFiddle so we can better help
0

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

Comments

0

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();

Comments

0
       <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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.