I am using jQuery to send a request to my server and then render the results. However, now I want to use a button, so that the request would only be sent after clicking on the send button, not like my previous code where I used an "onkeyup" which is uncontrollable; because I want to send my request after finishing typing my request and not before.
<script>
function Search(query_text)
{
$.get( "http://localhost:9000?query="+query_text, function( data ) {
$(\"#div2\").empty()
..............
$(\"#div2\").append("</table>")
}, "json")
.fail(function() {
$("#rep")
.append('<br/><p style="color:red">Oops! Error with XMLHttpRequest</p>')
});
;
}
</script>
This is the input that calls the jQuery function:
<input id="queryText" type="text" onkeyup="Search($(this).val())" /><br>
I've tried to add a simple button and then call the jQuery function like this, but it didn't work because the page reloads and I don't get the results I need:
<form class="form-inline">
<div class="form-group">
<input type="text" class="form-control" id="queryText" placeholder="Your text">
</div>
<button type="submit" class="btn btn-primary" onclick="Search($('#queryText').val())">Find courses</button>
</form>
I don't know if there is a way to keep using GET requests instead of using a form and sending that time a POST request.
Thank you in advance!