Rohit
Rohit

Reputation: 705

how to set a alert if there is no matches found in search in jquery

I am implementing a search functionality .I found a link it is working fine ..But one one issue is there it is not set a alert when there is no match found in page.Can you please tell me where is insert that alert .I debug no alert coming. I done like that .match.length() ==0 then i show alert but it is not working can you please help ?

here is the link where i found code. http://jsbin.com/umodoy/7/edit

var searchIndex = -1;
var searchTermOld ='';

$(document).ready(function(){
  $('.searchbox').on('change',function(){
    if($(this).val()===''){
      var  selector= "#realTimeContents";
     $(selector+' span.match').each(function(){
        $(this).replaceWith($(this).html());
      });
    }
      searchIndex = -1;
      $('.searchNext').attr("disabled", "disabled");
      $('.searchPrev').attr("disabled", "disabled");
    searchTermOld = $(this).val();
  });
  $('.searchbox').on('keyup',function(){

    var  selector= "#realTimeContents";
    if($(this).val()===''){
     $(selector+' span.match').each(function(){
        $(this).replaceWith($(this).html());
      });
    }
    if($(this).val() !== searchTermOld){
     $(selector+' span.match').each(function(){
        $(this).replaceWith($(this).html());
      });
      searchIndex = -1;
      $('.searchNext').attr("disabled", "disabled");
      $('.searchPrev').attr("disabled", "disabled");                              
  }
  });
  $('.search').on('click',function(){
    if(searchIndex == -1){
      var searchTerm = $('.searchbox').val();
      searchAndHighlight(searchTerm);
    }
    else searchNext();
    if($('.match').length >1){
      $('.searchNext').removeAttr("disabled");
      $('.searchPrev').removeAttr("disabled");
    }
  });
  $('.searchNext').on('click',searchNext);

  $('.searchPrev').on('click',searchPrev);
});

function searchAndHighlight(searchTerm) {
    if (searchTerm) {
        var searchTermRegEx, matches;
        var  selector= "#realTimeContents";
        $(selector+' span.match').each(function(){
        $(this).replaceWith($(this).html());
      });
        try {
            searchTermRegEx = new RegExp('('+searchTerm+')', "ig");
        } catch (e) {
            return false;
        }
        $('.highlighted').removeClass('highlighted');
        matches = $(selector).text().match(searchTermRegEx);
        if (matches !==null && matches.length > 0) {
            var txt = $(selector).text().replace(searchTermRegEx, '<span class="match">$1</span>');
            $(selector).html(txt);
            searchIndex++;
            $('.match:first').addClass('highlighted');
           $(document).scrollTop($('.match').eq(searchIndex).offset().top);

          return true;
        }
      return false;
    }
  return false;
}

function searchNext(){
  searchIndex++;
  if (searchIndex >= $('.match').length) searchIndex = 0;
  $('.highlighted').removeClass('highlighted');
  $('.match').eq(searchIndex).addClass('highlighted');
  $(document).scrollTop($('.match').eq(searchIndex).offset().top);
}

function searchPrev(){
  searchIndex--;
  if (searchIndex < 0) searchIndex = $('.match').length - 1;
  $('.highlighted').removeClass('highlighted');
  $('.match').eq(searchIndex).addClass('highlighted');
  $(document).scrollTop($('.match').eq(searchIndex).offset().top);
}

Upvotes: 0

Views: 325

Answers (3)

Working Demo http://jsbin.com/umodoy/29/edit

function searchAndHighlight(searchTerm) {
    if (searchTerm) {
        var searchTermRegEx, matches;
        var  selector= "#realTimeContents";
        $(selector+' span.match').each(function(){
        $(this).replaceWith($(this).html());
      });
        try {
            searchTermRegEx = new RegExp('('+searchTerm+')', "ig");
        } catch (e) {
            return false;
        }
        $('.highlighted').removeClass('highlighted');
        matches = $(selector).text().match(searchTermRegEx);
        if (matches !==null && matches.length > 0) {
            var txt = $(selector).text().replace(searchTermRegEx, '<span class="match">$1</span>');
            $(selector).html(txt);
            searchIndex++;
            $('.match:first').addClass('highlighted');
           $(document).scrollTop($('.match').eq(searchIndex).offset().top);

          return true;
        }else{ //added this else here
          alert('not found');
        }
      return false;
    }
  return false;
}

for scrolling problem js

Working Demo http://jsbin.com/umodoy/37/edit

replace scrolling code by this

if($('.match').eq(searchIndex).offset().top > $(window).height()-10){
    $(document).scrollTop($('.match').eq(searchIndex).offset().top);
}

Upvotes: 2

Sergio
Sergio

Reputation: 28837

Demo here
You can add it after the end of the if that returns true.

Like:

  return true;
    }
  alert('no matches!');
  return false;

This if (matches !==null && matches.length > 0) searches for match different from NULL and returns true, which makes the function stop. If that statement is not met/true, you can place the alert after that if.

Upvotes: 1

Moti Korets
Moti Korets

Reputation: 3748

Just check return value of searchAndHighlight if its false show alert.

if(!searchAndHighlight(searchTerm))
    alert('No Matches Found.');

Upvotes: 1

Related Questions