UnKnown UnKnown - 1 month ago 11
jQuery Question

How to display No Records Found Message for div searh on keyup

I am searching the div as user enters input .
Incase there are no records i should display a message a No Records

Incase i press 'H' inside search its initially displaying message as 'No Records '

This is my js code

$('#searchequip').keyup(function()
{
var val = $.trim(this.value).toUpperCase();
$(".mt-checkbox").each(function()
{
var parent = $(this).closest('li'),
length = $(this).text().length > 0;
if (length && $(this).text().search(new RegExp(val, "i")) < 0)
{
parent.fadeOut("slow");
console.log('Nothing Found ');
$("#errmsg").html("No Results Found").show().fadeOut("slow");
}
else
{
parent.show();
}
});
})


This is my fiddle

http://jsfiddle.net/cod7ceho/228/

Answer

This is the code; http://jsfiddle.net/cod7ceho/246/

$('#searchequip').keyup(function (){
  var val = $.trim(this.value).toUpperCase();
  var noElem = true;
  $('.mt-checkbox').each(function (){
    var parent = $(this).closest('li'),
    length = $(this).text().length > 0;
    if (length && $(this).text().search(new RegExp(val, 'i')) < 0)
    {
      parent.fadeOut('slow');
    }else{
      noElem = false;
      parent.show();
    }
  });
  if (noElem)
    $('#errmsg').html('No Results Found').show().fadeOut('slow');
})
Comments