0

I have written autocomplete function using keyup but when clicking outside autofill dropdown doesnt hide. autofill search in database and fill the data and show it. i have used wordpress

jQuery("#edit-full-name").keyup(function(e) {
    jQuery("#fullnamelist").empty();
    if (e.which == 8 || e.which == 46) return false;

    jQuery.ajax({
        type: "POST",
        url: my_ajax_object.ajax_url,
        dataType: "json",
        data: {
            action: 'autocomplete_name',
            text: jQuery(this).val(),
        },
        beforeSend: function() {
            jQuery("#edit-full-name").css("background", '#FFF url(' + my_ajax_object.theme_path + '/LoaderIcon.gif) no-repeat 165px');
        },
        complete: function() {
            jQuery("#edit-full-name").css("visibility", "hidden");
        },
        cache: false,
        success: function(data) {
            jQuery("#fullnamelist").empty();
            jQuery.each(data, function(i, obj) {
                jQuery("#fullnamelist").append('<li>' + obj + '</li>');

            })
            jQuery("#fullnamelist li").bind("click", function() {
                setText(this);
            });
        },
        complete: function() {
            jQuery("#edit-full-name").removeAttr("style");
        }
    })
})
Yash Karanke
  • 764
  • 1
  • 15
  • 29
mike
  • 1
  • 2
  • Hi! Welcome to StackOverflow! Please take a look at tour https://stackoverflow.com/tour to get a better understanding about https://stackoverflow.com/help/how-to-ask. Another good read: https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions?. Afterwards, please edit your question to add all the relevant code – Mohit Sharma Jul 07 '22 at 13:40
  • I don't see any code attempting to hide the dropdown when the user clicks somewhere else. The usual technique is to set a click handler on the body and make sure that the click did not happen inside your popup. See [Detect click inside/outside of element with single event handler](https://stackoverflow.com/questions/4660633/detect-click-inside-outside-of-element-with-single-event-handler) for an example. Once you have tried something and it didn't work, then you can post a question explaining what you have tried and what is not behaving as expected. See http://idownvotedbecau.se/noattempt/ – Ruan Mendes Jul 07 '22 at 13:51

0 Answers0