Skip to Content
Menu

autocompleteSearchListeners()

Enable autocomplete search on core WordPress input field selectors.

JavaScript February 16, 2021

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Examples

Prevent default Nebula autocomplete on a search field. This is also useful for overriding the default Nebula autocomplete in favor of customized parameters.

HTML
<input id="s" class="no-autocomplete" type="text" placeholder="Search this website..." />

Additional Notes

This function runs on all input fields with an ID of s or a class of search.

To prevent an autocomplete on a certain input, use the class no-autocomplete.

 

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/search.js on line 95.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use wp.hooks.doAction() and wp.hooks.addFilter() in your JavaScript file.

    Filters
    "nebulaAutocompleteSearchSelector"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    JavaScript
    nebula.autocompleteSearchListeners = async function(){
        let autocompleteSearchSelector = wp.hooks.applyFilters('nebulaAutocompleteSearchSelector', '.nebula-search input, input#s, input.search');
        jQuery(autocompleteSearchSelector).one('focus', function(){ //Only do this once
            if ( !jQuery(this).hasClass('no-autocomplete') ){ //Use this class to disable or override the default Nebula autocomplete search parameters
                nebula.loadJS(nebula.site.resources.scripts.nebula_jquery_ui, 'jquery-ui').then(function(){
                    nebula.dom.document.on('blur', '.nebula-search input', function(){
                        jQuery('.nebula-search').removeClass('searching').removeClass('autocompleted');
                    });
    
                    //I do not know why this cannot be debounced
                    jQuery('input#s, input.search').on('keyup paste', function(e){
                        let allowedKeys = ['Backspace', 'Delete']; //Non-alphanumeric keys that are still allowed to trigger a search
    
                        if ( jQuery(this).val().trim().length && (nebula.isAlphanumeric(e.key, false) || allowedKeys.includes(e.key) ) ){
                            let types = false;
                            if ( jQuery(this).is('[data-types]') ){
                                types = jQuery(this).attr('data-types');
                            }
    
                            nebula.autocompleteSearch(jQuery(this), types);
                        } else {
                            jQuery(this).closest('form').removeClass('searching');
                            jQuery(this).closest('.input-group, .nebula-input-group').find('.fa-spin').removeClass('fa-spin fa-spinner').addClass('fa-search');
                        }
                    });
                });
    
                nebula.loadCSS(nebula.site.resources.styles.nebula_jquery_ui);
            }
        });
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name.

    JavaScript
    nebula.autocompleteSearchListeners = function(){
        //Write your own code here, leave it blank, or return false.
    }