Skip Navigation

[Resolved] ajax search with chosen or select2 parametric search

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 4 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1548203

Tell us what you are trying to do?
Since the latest update my ajax updated parametric search with chosen multiselect isn't working anymore.

In view HTML:

<select name="wpv-lehrinhalt[]" class="chosen-select js-wpv-filter-trigger form-control" multiple="multiple" size="10" style="display: none;"></select>

In function.php:

/**
 * Enqueue scripts and styles.
 */
function education_scripts() {

/* Scripts */
	wp_enqueue_script( 'semantic', '/wp-content/themes/wuuzilla-education/semantic-ui/semantic.min.js', array(), $ver = false , $in_footer = false );
	wp_enqueue_script( 'chosen', '/wp-content/plugins/types/vendor/toolset/toolset-common/res/lib/chosen/chosen.jquery.min.js', array(), $ver = false , $in_footer = false );
	wp_enqueue_script( 'select2', '/wp-content/plugins/types/vendor/toolset/toolset-common/res/lib/select2/select2.min.js', array(), $ver = false , $in_footer = false );
	wp_enqueue_script( 'script', '/wp-content/themes/wuuzilla-education/js/script.js', array(), $ver = false , $in_footer = false );

	/* Styles */
	wp_enqueue_style( 'semantic.css', get_stylesheet_directory_uri().'/semantic-ui/semantic.min.css' );
	wp_enqueue_style( 'theme.css', get_stylesheet_directory_uri().'/semantic-ui/theme.css' );
	wp_enqueue_style( 'chosen.css', get_stylesheet_directory_uri().'/chosen/chosen.css' );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'education_scripts' );

In script.js:

jQuery( document ).on( 'js_event_wpv_parametric_search_triggered', function( event, data ) {
	/**
	* data.view_unique_id (string) The View unique ID hash
	*/
	
	/* Chosen */	
 $(".chosen-select").chosen({
no_results_text: "Leider kein Treffer!",
max_selected_options: 5,
placeholder_text_multiple: "Suche..."

 }); 

Error occuring:

Uncaught TypeError: $ is not a function
for the line $(".chosen-select").chosen({}):

Website hidden link

Thanks for your help!

#1548707

Hello, the error says you cannot use the $ namespace, so try using the jQuery namespace instead. Current code:

$(".chosen-select").chosen({

Updated code:

jQuery(".chosen-select").chosen({
#1548711

Hallo Christian,

I already tried. Then the error says:

jQuery(.....) is not a function.

Regards
Benjamin

#1548923

Can you show me exactly where you see this error? I am able to use the developer tools to change $ to jQuery in scripts.js, but I don't see the same error. I must not understand where you see the error. What I see is you have many instances where $ must be replaced with jQuery.

Here is a video showing you my debug process:
hidden link

If you can show me in a screenshot or video where the "jQuery is not a function" error is triggered, I will take a closer look. Thank you!

#1549339

Thanks for the help!

I changed every $ to jQuery, now my search works again. Was there a change? The code worked before the update with $ too?

Regards Benjamin

#1551557

I'm not aware of anything specific in Toolset related to the jQuery namespace, but so many different plugins and libraries use jQuery that it would be difficult for me to track down exactly what caused this in your site. It could be something as simple as the order in which some dependencies are loaded, or the position of some code in the page markup. Regardless, it's always a good idea to use the jQuery namespace with Toolset plugins, or use a callback of some kind that provides access to the $ namespace in a closure:

jQuery(document).ready(function($) {
  // it is okay to use $ here
});

// it is best to use jQuery here instead of $