Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1226463

    is possible to have an accordion open on desktop and closed on smarthphone?

    #1227477

    Hey weasyweb2015,

    Thank you for the inquiry.

    This should be possible by creating a custom script. Please try to add this snippet 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);
    

    This will close the open toggle on smaller screens.

    Best regards,
    Ismael

    #1421547

    Chatp GPT-4 has got me going! :D

    function custom_header_code() {
        ?>
    	<script type="text/javascript">
    			
    		document.addEventListener('DOMContentLoaded', (event) => {
    			if (window.innerWidth <= 600) {
    				const div = document.querySelector('div#toggle-id-1');
    				const p = document.querySelector('.toggler');
    				if(div || p) {
    					div.classList.remove('active_tc');
    					p.classList.remove('activeTitle');
    				}
    			}
            });
    
    	</script>
        <?php
    }
    add_action( 'wp_head', 'custom_header_code' );
    • This reply was modified 1 year, 2 months ago by csabaio.
    #1421570

    Hi csabaio,

    I’m glad Chat GPT-4 has helped you :D
    Let us know if you need further assistance.

    Best regards,
    Nikko

    #1421589

    Nice – but maybe it is best to select the div by its active state – because not always the first toggle is opend at start:

    
    function custom_header_code() {
    ?>
    <script type="text/javascript">    
        document.addEventListener('DOMContentLoaded', (event) => {
          if (window.innerWidth <= 768) {
            const div = document.querySelector('div.active_tc');
            const p = document.querySelector('.activeTitle');
            if(div || p) {
              div.classList.remove('active_tc');
              p.classList.remove('activeTitle');
            }
          }
        });
    </script>
    <?php
    }
    add_action( 'wp_head', 'custom_header_code' );

    and if you really want to have it only on mobile devices – you can have that without screen width detection.

    function custom_header_code() {
    if(wp_is_mobile()){
    ?>
    <script type="text/javascript">   
    window.addEventListener("DOMContentLoaded", function () { 
            const div = document.querySelector('div.active_tc');
            const p = document.querySelector('.activeTitle');
            if(div || p) {
              div.classList.remove('active_tc');
              p.classList.remove('activeTitle');
            }
    });
    </script>
    <?php
    }}
    add_action( 'wp_head', 'custom_header_code' );

    only if you choose the selector activeTitle – the symbol in front will be set to closed ( + )

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