Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1275227

    Dear all,

    I am using the accordion element (FAQ-style) and would like to set the attribute “behaviour – initial open” in relation to the screen size of the decive. On mobile screens, I would like it to be “0” (i.e., all accordion toggles closed on page load). Whereas on desktop, I would like it to have the value “1” (i.e., first accordion toggle open on page load).

    Do you probably have some code for this?

    Many thanks in advance.

    Best,

    Hagen

    #1275722

    Hey HagenWittig,

    Thank you for the inquiry.

    We could close the active item on mobile view by clicking it using a custom script, or by dispatching a click event on that particular item. Please try to add this code in the functions.php file.

    add_action('wp_footer', function() {
    ?>
    <script>
    document.addEventListener("DOMContentLoaded", function() {
         if(window.innerWidth > 769) return;
         document.getElementsByClassName("activeTitle")[0].dispatchEvent(new Event('click'));
    });
    </script>
    <?php
    }, 9999);
    

    The code should work when the screen width of the device is less than 769px. Let us know if it helps.

    Best regards,
    Ismael

    #1275868

    Dear Ismael,
    thank you very much. Your solution looks good to me, however, it doesn’t seem to work on my end… ;-)
    The particular site I want it to work for is:
    https://www.dental.one/terminvereinbarung-neu2
    Feel free to check it yourself. The two accordion toggles should be closed on page load on mobile devices. On desktop, I want them to be open on page load.
    Further, your code should only influence this particular site and not the rest of the website. Can we add something to solve this?
    Thanks again and best regards,
    Hagen

    #1275969

    Hi,

    Thank you for the update.

    We cannot find the recommended script in the document or page. Did you remove it? Please add the script again so that we could check if it is actually working or if there is any error. Please make sure to copy the code directly from the forum and not from your email.

    Best regards,
    Ismael

    #1276110
    This reply has been marked as private.
    #1276236

    Hi,

    Thank you for the info.

    We just noticed that all toggler are opened on page load, so we adjusted the script in the functions.php file a bit to loop through all togglers and close all of them on mobile view. This is the final code.

    /*-------------------------------------------------------------
    // Close open accordion toggles on mobile devices
    //------------------------------------------------------------*/
    
    add_action('wp_footer', function() {
    ?>
    <script>
    document.addEventListener("DOMContentLoaded", function() {
         if(window.innerWidth > 769) return;
    	 setTimeout(function() {		 
    		 var actives = document.getElementsByClassName("toggler");
    		 Array.prototype.forEach.call(actives, el => {
    			el.dispatchEvent(new Event('click'));
    			el.classList.remove("activeTitle");
    		});
    	 }, 500);
    });
    </script>
    <?php
    }, 9999);

    Best regards,
    Ismael

    #1276316

    Dear Ismael,
    This is working beautifully. Wow! Thank you so much.
    Your script is now executed on all pages of the website. Do you have some additional code to limit its execution to this particular site (i.e., “terminvereinbarung-neu2”)?
    Thanks again and best regards,
    Hagen

    #1276327

    Hi,

    You are very welcome! Glad it is working.

    To prevent the script from executing on other pages, please look for this line in the script above.

    if(window.innerWidth > 769) return;
    

    Below that, add this code.

     var terminvereinbarung= document.querySelector(".page-id-3779");
      if(!terminvereinbarung) return;
    

    Best regards,
    Ismael

    #1276597

    Dear Ismael,
    Fantastic! The page now is looking and behaving like I wanted it to do. Thank you!
    Best,
    Hagen

    #1276635

    Hi,

    I’m glad this was resolved. If you need additional help, please let us know here in the forums.

    Best regards,
    Jordan Shannon

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.