Custo, Js doesn't work if enqueued

Hi there, in the past you have give me a piece of js code to execute a function that I’ve needed.

The code is this:

jQuery(document).ready(function($) 
{
var hash = window.location.hash.substr(1);
if(hash){
    jQuery('[data-vc-grid-filter-value=".'+hash+'"]').trigger('click');
}
});

It works perfectly if it is added in custom Js in x-theme customization options. But putting the code there it can’t be executed when some plugins like Autoptimize are active.
So, I have created a dedicated script and enqueued correctly through my function.php file. See below:

function enqueue_vc_script() {
if (is_page(array('home', 'home-en', 'home-de', 'design-progetti')) ) {
    wp_enqueue_script( 'vc-script', get_stylesheet_directory_uri() . '/js/vc-script.js' , array('jquery'), '1.0.0', true );
}
}

add_action( 'wp_enqueue_scripts', 'enqueue_vc_script' );

vc_script.js content reflects the code you have provide to me:

/*Script for Visual Composer post category link*/

jQuery(document).ready(function($) 
{
var hash = window.location.hash.substr(1);
if(hash){
    jQuery('[data-vc-grid-filter-value=".'+hash+'"]').trigger('click');
}
});

And the script is loaded correctly on the frontend:

So the script is active. But in this way that piece of code doesn’t work at all.

Maybe that code has to be changed to work in a enqueued script?

Let me know, thanks

Hi Alessandro,

Thanks for reaching out.
It might be some customization or 3rd Party plugin issue, which is beyond the scope of theme support. I would suggest you hire a developer who can assist you on this or you can avail of our newly launched service called One, where we answer the questions beyond normal theme support.
Please remember that we don’t offer any investigation related the custom coding.

Thanks

Hi, custom code you have seen was provided by you in the past. It’s not related to a third party plugin. The code help to reach a particular category of a Visual composer gallery.

Hi Alessandro,

I am not referring to the custom code written to enqueued the script, it might be some other custom code and some 3rd Party plugin which blocks the previous code to work.

Thanks

Ok, thanks. It’s not simple to understand what could be.

Hey Alessandro,

We’re sorry to hear that, but the code coming from our forum just serves as a starting point. Issues that might arise from the use of custom JS code and further enhancements should be directed to a third-party developer or you can avail One where we can answer questions outside of the features of our theme.

Thank you for understanding.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.