Javascript API
The javascript API consists of two parts, one is the events triggered by the plugin, and methods, which you can use to trigger specific events.
Events list
The events are fired on the main search container element which you can target with ".asp_main_container" class selector. For example attaching an event handler before the search starts:
Common arguments
All of the events will always pass on these three arguments (+ additional if defined):
event (object) - a common javascript event object, always passed in event handlers
id (integer) - the search ID. This is the same as the one in the shortcode.
instance (integer) - the instance number of the current search ID. This will help if the same search shortcode is used multiple times on the same page.
When using native javascript, the event arguments can be found in the event handlers argument, in the event.detail array. See the example above.
asp_init_search_bar
Triggers when the search search instance is initialized.
asp_settings_show
Triggers when the search settings are displayed.
asp_settings_hide
Triggers when the search settings get hidden.
asp_results_show
Triggers when the search results are displayed.
asp_results_hide
Triggers when the search results get hidden.
asp_elementor_results
Triggers when the search settings are displayed.
Additional arguments
element (Element) - The node object
asp_search_start
Triggers right before the search is started.
Additional arguments
s (string) - The search phrase
asp_search_end
Triggers right after the search is finished.
Additional arguments
s (string) - The search phrase
data (string) - HTML data returned by the server
Sample implementation to the functions.php file in theme/child theme directory
Triggering Events/Functions
To call a pre-defined event, use either version of the following function:
Function arguments
id (integer) - the search ID. This is the same as the one in the shortcode. If id=0, then the call applies to all visible search instances on the page.
instance (integer) - the instance number of the current search ID. This will help if the same search shortcode is used multiple times on the same page.
function_name - the event/function you want to trigger
args - arguments passed to the triggered function
Functions list
searchFor
Triggers the search for a specific search phrase.
Arguments
s (string) - the search phrase
Examples
searchRedirect
Triggers the search redirection to the results page (as configured on the back-end) using the given search phrase.
Arguments
s (string) - the search phrase
Examples
toggleSettings
Toggles the search settings container state to either visible or invisible.
Arguments
state (string) - 'show' or 'hide'
Examples
closeResults
Closes the results box
Arguments
clear (bool) - Clears the search input, when true. Defaults to false.
Examples
getStateURL
Returns a URL for the currently viewed page, with the search settings preset to the current state. Visiting this URL will preset the search settings to that state.
You can learn more about this function here.
Examples
Examples
Search
Search for a specific phrase on search ID 1:
Similarly, but only on instance 1:
Search all visible instances:
Settings
Toggle search settings box on/off:
Explicitly show/hide the settings:
Results
To hide the results window:
Current search state URL - presetting the search filters and input to a specific state
There is a way to get the current filters and search input state on the current page, and generate a URL - which presets the search to this state.
This returns a string like:
..visiting this URL will preset the search filters to this state.
Last updated