Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make the dropdown autocomplete to disappear onblur or click outside in jquery?

Tags:

jquery

Below is the function which is called to show the autocomplete in a search box...I want the auto complete to disappear onBlur or click outside the search box...please tell me what should be added in this function to make the autocomplete drop down disappear when clicked outside?

function hideLoader(){
        $('#sub_cont').fadeIn(100);
        $('.search-background').fadeOut(100);
    };

    $('#search').keyup(function(e) {
      if(e.keyCode == 13) {
        showLoader();
        $('#sub_cont').fadeIn(100);
        $("#content #sub_cont").load("../ajax/search.php?val=" + $("#search").val(), hideLoader());
        }
      });

//Fading out the div when the text box is empty
    String.prototype.trim = function() {
    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        }

    $(".searcher").keyup(function(){

        if($('.searcher').val().trim().length > 0) {
            // is not empty
            showLoader();
            $('#sub_cont').fadeIn(100);
            $("#content #sub_cont").load("../ajax/search.php?val=" + $("#search").val(), hideLoader());
        } else {
            // is empty
            $('#sub_cont').fadeOut(100);
        }

    });
like image 897
Bala Avatar asked Oct 25 '25 23:10

Bala


1 Answers

This is a bit simpler:

$('body').click( function() {
    $('.ui-autocomplete').hide('');

});
like image 100
Jason Avatar answered Oct 29 '25 05:10

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!