Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autosuggest to dynamically loaded input fields #2387

Closed
felipeelia opened this issue Oct 5, 2021 · 1 comment · Fixed by #2404
Closed

Add autosuggest to dynamically loaded input fields #2387

felipeelia opened this issue Oct 5, 2021 · 1 comment · Fixed by #2404
Assignees
Milestone

Comments

@felipeelia
Copy link
Member

Is your enhancement related to a problem? Please describe.

Currently, autosuggest only works with input fields already in the DOM. For inputs dynamically added at a later time, via JavaScript, autosuggest won't work.

Describe the solution you'd like

It would be great if we could (1) retrigger the autosuggest init function OR (2) change the way autosuggest works, instead of adding the behavior to each field, adding it to the document body, checking which element started the event (props to @JakePT for this idea.)

Describe alternatives you've considered

As a workaround, it seems users wanting to have this working with the current codebase could add the .js dynamically later.

@felipeelia
Copy link
Member Author

This is an example of the workaround mentioned in the issue description:

/**
 * Avoid outputting the `<script>` tag but don't prevent EP of creating
 * the `epas` object through `wp_localize_script()`
 */
add_filter(
	'script_loader_tag',
	function( $script, $handle ) {
		if ( 'elasticpress-autosuggest' === $handle ) {
			$script = '';
		}
		return $script;
	},
	10,
	2
);

/**
 * After the DOM is loaded create a new form with an input (autosuggest inputs need to be inside a form).
 * After that, add the Autosuggest JS to the DOM. Weighting here is `999` so autosuggest's `epas` object
 * is already created.
 */
add_action(
	'wp_footer',
	function() {
		if ( ! defined( 'EP_URL' ) ) {
			return;
		}
		?>
		<script>
			document.addEventListener("DOMContentLoaded", function() {
				// Create a form with an input inside of it.
				var form = document.createElement('form'),
					input = document.createElement('input');
				input.type = 'search';
				form.appendChild(input);
				document.getElementById('masthead').appendChild(form);

				// Dynamically load the autosuggest JS
				var s = document.createElement( 'script' );
				s.setAttribute( 'src', '<?php echo esc_url( EP_URL . 'dist/js/autosuggest-script.min.js?ver=' . EP_VERSION ); ?>' );
				document.body.appendChild( s );
			});
		</script>
		<?php
	},
	999
);

@mckdemps mckdemps added this to the 3.6.4 milestone Oct 5, 2021
@felipeelia felipeelia linked a pull request Oct 18, 2021 that will close this issue
6 tasks
felipeelia added a commit that referenced this issue Oct 19, 2021
Support Autosuggest on dynamic elements. Addresses #2387.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants