I have a search box which should work with pressing the button and also pressing the Enter key. They both have the same code. The button works perfectly but when it comes to the Enter key, most of the time it doesn't work and sometimes it suddenly works.
Here's my code:
$(document).ready(function() {
function myFunction() {
$("#search-criteria").keydown(function(event) {
if (event.keyCode === 13) {
var txt = $('#search-criteria').val();
$('.items:contains("' + txt + '")').addClass('searched');
}
});
}
$('#search').click(function() {
var txt = $('#search-criteria').val();
$('.items:contains("' + txt + '")').addClass('searched');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="text" id="search-criteria" onkeydown="myFunction()" name="search" placeholder="Search..">
<input type="button" id="search" value="search" />
</form>
Remove the inline-event onkeydown
you don't need it.
You need to use preventDefault
inside your myFunction
function.
Remove the classes inside your functions to see clearly the new search result.
$(document).ready(function() {
$("#search-criteria").keypress(function(event) {
if (event.which === 13) {
myFunction();
}
});
//code for the button
$('#search').click(myFunction);
});
var myFunction = function() {
event.preventDefault();
var txt = $('#search-criteria').val();
$('.items').removeClass('searched');
$('.items:contains("' + txt + '")').addClass('searched');
}
.searched {
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="text" id="search-criteria" name="search" placeholder="Search..">
<input type="button" id="search" value="search" />
</form>
<ul>
<li class="items">Coffee</li>
<li class="items">Tea</li>
<li class="items">Milk</li>
<li class="items">Coffee</li>
<li class="items">Tea</li>
<li class="items">Milk</li>
<li class="items">Coffee</li>
<li class="items">Tea</li>
<li class="items">Milk</li>
</ul>
I'm trying to do something I don't remember/find if it is possible in javascript. I have an object constructor "Point" and I want to know if it possible to, for example, sum two different objects ...
I'm trying to do something I don't remember/find if it is possible in javascript. I have an object constructor "Point" and I want to know if it possible to, for example, sum two different objects ...
I'm trying to start a git repo when scaffolding an application with a yeoman custom generator, this is what I got at the install step: install: function () { this.installDependencies(); ...
I'm trying to start a git repo when scaffolding an application with a yeoman custom generator, this is what I got at the install step: install: function () { this.installDependencies(); ...
I created an array buffer by var uni = new Uint8Array([255, 216, 255, 0, 0, 0, 0, 0]) But when I try to get back the bytes using map uni.map(byte => byte.toString(16)) it returns Uint8Array(8) ...
I created an array buffer by var uni = new Uint8Array([255, 216, 255, 0, 0, 0, 0, 0]) But when I try to get back the bytes using map uni.map(byte => byte.toString(16)) it returns Uint8Array(8) ...
I have a checkbox column inside Ext.grid.EditorGridPanel configured like this: { id: 'event-type-grid-enabled-checkbox', header: "Enabled", dataIndex: "enabled", sortable: true, ...
I have a checkbox column inside Ext.grid.EditorGridPanel configured like this: { id: 'event-type-grid-enabled-checkbox', header: "Enabled", dataIndex: "enabled", sortable: true, ...