• Resolved fredericv

    (@fredericv)


    Hello,

    I need to add some extra javascript/jquery scripts after the loading of post-cards in my page to manipulate them. I’m loading my script files from functions.php in wp_enqueue_script function.

    But it appears that my files are loaded before the end of the post-cards loading, so I can’t manipulate the post-cards, because my JS files don’t ‘see’ them, they are not loaded in the DOM. Even jquery’s $(document).ready() is blind !

    Do you know how to fix it ?

    Thank you very much !

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author YMC

    (@wssoffice21)

    Hi!
    The filter’s post grid works asynchronously, not synchronously, so your javascripts don’t see the loaded posts. If you need to interact with the loaded posts, you should use hooks:

    ymcHooks.doAction('ymc/grid/after_update', data, container);ymcHooks.doAction('ymc/grid/after_update_filter_id', data, container);ymcHooks.doAction('ymc/grid/after_update_filter_id_instance_index', data, container);

    For example:

    ymcHooks.addAction('ymc/grid/after_update', function(data, container) {
    console.log('Posts are inserted into the DOM:', container);
    console.log('Server response data:', data);
    });

    OR

    ymcHooks.addAction('ymc/grid/after_complete', function(status, container) {
    if (status === 200) {
    console.log('The request was completed successfully.');
    }
    });

    We recommend adding a filter ID (filter_id) to the hook; this will allow you to avoid global application to all pages of the site. This will allow you to access the post card. Please check out the full list of hooks in the plugin documentation: https://github.com/YMC-22/YMC-Filter#javascript-integration-hooks

    We hope this helps. We were happy to help!

    Thread Starter fredericv

    (@fredericv)

    Ok, thanks.

    I’ve found a solution with this code :

    function waitForElement(selector, callback) {
    const observer = new MutationObserver((mutations, observer) => {
    const element = document.querySelectorAll(selector);
    if (element) {
    observer.disconnect();
    callback(element);
    }
    });

    observer.observe(document.body, {
    childList: true,
    subtree: true,
    });
    }

    then I use :

    waitForElement('article.post-card', (element) => {
    console.log('Element exists:', element);

    });

    But I will try your solution.

    Where do you put ymc hooks ?

    Plugin Author YMC

    (@wssoffice21)

    For reliability, you should use plugin hooks. You can add this code to your JavaScript files, for example in the section:

    document.addEventListener("DOMContentLoaded", () => {
    ymcHooks.addAction('ymc/grid/after_update', function(data, container) {
    console.log('Posts are inserted into the DOM:', container);
    console.log('Server response data:', data);
    // Your code...
    });
    });

    Don’t forget to specify the filter ID in the function, for example:
    ymcHooks.addAction('ymc/grid/after_update_72', function(data, container) { ..})
    Here, 72 is the ID of your filter on the page!

    • This reply was modified 14 hours ago by YMC.
    • This reply was modified 13 hours, 57 minutes ago by YMC.
    • This reply was modified 13 hours, 56 minutes ago by YMC.
    Thread Starter fredericv

    (@fredericv)

    Ok, I put it in a javascript file, in jquery’s $(document).ready() function, but there’s an error :

    The hook name can only contain numbers, letters, dashes, periods and underscores.
    Thread Starter fredericv

    (@fredericv)

    It works in document.addEventListener(“DOMContentLoaded”)

    Does it work within JQuery ?

    Plugin Author YMC

    (@wssoffice21)

    Send us your JavaScript code. You probably added a bug somewhere )

    Plugin Author YMC

    (@wssoffice21)

    Yes, it should work inside $(document).ready(), but we recommend you use native JavaScript, it’s more reliable!

    Thread Starter fredericv

    (@fredericv)

    Ok, I will do that 🙂

    Many thanks !

    Plugin Author YMC

    (@wssoffice21)

    Good Luck!

    Thread Starter fredericv

    (@fredericv)

    Just to say : your support is the best plugin support I have ever seen 🙂 !! BRAVO !!

    Plugin Author YMC

    (@wssoffice21)

    Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.