I'm currently using a client-side input box* that filters search results based on text input automatically; with no dependency on server side functions, in this case the return/enter key, (which would just refresh the page) so I have disabled it using:
<script>
var submitHandler = function() {
// do stuff
return false;
}
</script>
This served it's initial purpose, however, upon hitting the return/enter key, (using the code above) the text field should be deselected as well, is there a way I can achieve this?
*source: http://www.redotheweb.com/2013/05/15/client-side-full-text-search-in-css.html
View: JSFiddle
Edit: Added Fiddle ^
add the below script above your form
<script>
function enter_detector(e) {
// if enter key is pressed lose focus
if(e.which==13||e.keyCode==13){
this.blur();
}
}
//add a listener for input box document.getElementById('search').addEventListener('keyup',enter_detector,false);
</script>
here is a snippet
<!-- Data generated by Faker, see https://github.com/fzaninotto/Faker -->
<ul class="contacts">
<!-- Add text to the data-index attribute to enable full-text search -->
<!-- Don't forget to lowercase it to make search case-insensitive -->
<li class="searchable" data-index="[email protected]">
<dl>
<dt>First Name</dt><dd>Ona</dd>
<dt>Last Name</dt><dd>Bednar</dd>
<dt>Email</dt><dd>[email protected]</dd>
<dt>Phone</dt><dd>1-265-479-1196x714</dd>
</dl>
</li>
<li class="searchable" data-index="[email protected](121)644-5577">
<dl>
<dt>First Name</dt><dd>Newton</dd>
<dt>Last Name</dt><dd>Cronin</dd>
<dt>Email</dt><dd>[email protected]</dd>
<dt>Phone</dt><dd>(121)644-5577</dd>
</dl>
</li>
<!-- add as much data as you want -->
</ul>
<form onsubmit="return false" class="form-search">
<input class="input-medium search-query" placeholder="search" id="search" type="text" onKey>
</form>
<script>
function enter_detector(e) {
// if enter key is pressed lose focus
if(e.which==13||e.keyCode==13){
this.blur();
}
}
document.getElementById('search').addEventListener('keyup',enter_detector,false);
</script>
<style id="search_style"></style>
<script type="text/javascript">
var searchStyle = document.getElementById('search_style');
document.getElementById('search').addEventListener('input', function() {
if (!this.value) {
searchStyle.innerHTML = "";
return;
}
// look ma, no indexOf!
searchStyle.innerHTML = ".searchable:not([data-index*=\"" + this.value.toLowerCase() + "\"]) { display: none; }";
// beware of css injections!
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With