I have two events: 1) keypress: it searches for data in an array. 2) click: for this event, a forced call of the keypress event should occur, which does not occur.
I use the trigger () method. In keypress, I use the condition for pressing Enter: if (event.keyCode == 13) {(all the search logic is here)} else {}. And as I already wrote, a keypress call with a click is not called. I assume that this is due to the condition event.keyCode == 13, but I could be wrong. Tell me, please, maybe in a function call you need to somehow register this condition (event.keyCode == 13) too? If so, tell me how, please. If this is not the problem, then what is the problem? I will be glad to any answer. From the code I deleted everything unnecessary for this question, leaving the basis. Thank.
$(document).on('keypress', '.search_edit', function(event){
let value = $(this).val().toLowerCase();
if (event.keyCode == 13) {
$('.blog_content').each(function() {
if($(this).text().toLowerCase().indexOf(value) === -1) {
$(this).addClass('item_none');
return;
} else {
$(this).removeClass('item_none');
}
});
});
$('.tags li a').on('click', function (event) {
event.preventDefault();
$('.search_edit').trigger('keypress');
});
////////////////////////////////////////// I tried to do like this. It didn’t help me.
$('.tags li a').on('click', function (event) {
event.preventDefault();
event.keyCode = 13;
$('.search_edit').trigger(jQuery.Event("keypress"));
});
You can do this by binding one single function to both the events.
and by passing your own data when binded with the click function as here there is no key press with the Enter key so your logic would not run as per your requirement.
Here is the code you can use:
$(document).on('keypress', '.search_edit', search);
function search(event){
event.preventDefault();
let value = $(this).val().toLowerCase();
if (event.keyCode == 13 || event.data.keyCode == 13) {
$('.blog_content').each(function() {
if($(this).text().toLowerCase().indexOf(value) === -1) {
$(this).addClass('item_none');
return;
} else {
$(this).removeClass('item_none');
}
});
}
$('.tags li a').on('click',{keyCode=13}, search);
Here I have passed the value of keyCode which is equal to 13 which would satisfy your condition.
or other way of doing this could be:
$(document).on('keypress', '.search_edit', search);
function search(event){
event.preventDefault();
let value = $(this).val().toLowerCase();
if (event.keyCode == 13 ) {
$('.blog_content').each(function() {
if($(this).text().toLowerCase().indexOf(value) === -1) {
$(this).addClass('item_none');
return;
} else {
$(this).removeClass('item_none');
}
});
}
$('.tags li a').on('click', function (event) {
event.preventDefault();
event.keyCode = 13;
search(event);
});