-
AuthorPosts
-
October 22, 2024 at 10:05 pm #1469647
Hi,
I’d like to open all accordion tabs with JavaScript on page opening. (The accordion is set to allow this.) The following code does not work:
const filtres = document.getElementsByClassName("toggler"); for (let i = 0; i < filtres.length; i++) { filtres[i].click(); }
Thanks,
SergeOctober 23, 2024 at 7:28 am #1469664Hey photographie-tous-azimuts,
Thank you for the inquiry.
Make sure that the Content > Behavior > Behavior is set to Multiple toggles open allowed, then add this script in the functions.php file to open all toggles on page load:
function ava_custom_open_all_toggles_script() { ?> <script> (function($) { $(document).ready(function() { $('.single_toggle .toggler').each(function() { var $this = $(this); if ($this.attr('aria-expanded') === 'false') { $this.click(); } }); }); }(jQuery)); </script> <?php } add_action( 'wp_footer', 'ava_custom_open_all_toggles_script', 99 );
Best regards,
IsmaelOctober 27, 2024 at 11:12 pm #1469956Hi Ismael,
I have copied the code to the single page where I want this behaviour. It worked, but the page ends up scrolled to the first accordion and the page URL has the #last-accordion-id tag. If I refresh the page, the tag is changed to #last-accordion-id-closed and there is an extra line at the bottom of the column containing the accordion.
Thanks,
SergeOctober 28, 2024 at 3:23 am #1469959Hi,
Thank you for the update.
I have copied the code to the single page where I want this behaviour.
Where did you add the script? This should be added in the functions.php file. Please provide a test page so that we can check the modification.
Best regards,
IsmaelOctober 28, 2024 at 9:52 pm #1470049Hi Ismael,
I added the script section of your code into a code block with a window.addEventListener call I already had for other purposes.
However, I did found a simpler solution: I now use accordion on small screens only (767px or less). On large screen, I use a simple code block with checkboxes only. This way, there is no need to make accordion open and deal with the resulting #tags that are added to the page URL when opening/closing accordion sections.
BTW, when I add code like the one below into accordion contents, the onClick parameter gets stripped out the next time I edit the accordion:
<div><input type="checkbox" class="filtre-de-cours" id="en-personne" name="en-personne" value="1" onclick="filtrerLesCours()"><label for="en-personne">En personne</label></div> <div><input type="checkbox" class="filtre-de-cours" id="en-ligne" name="en-ligne" value="2" onclick="filtrerLesCours()"><label for="en-ligne">En ligne</label></div> <div><input type="checkbox" class="filtre-de-cours" id="hybride" name="hybride" value="3" onclick="filtrerLesCours()"><label for="hybride">Hybride</label></div>
Thanks,
SergeOctober 29, 2024 at 8:05 am #1470073Hi,
Thank you for the update.
I added the script section of your code into a code block with a window.addEventListener call I already had for other purposes.
The code should be added in the function.php file. It can be added to a Code Block element but you have to remove the PHP code.
However, I did found a simpler solution
Great! Glad to know that you’ve got this working. Let us know if you have any more questions.
Best regards,
IsmaelOctober 29, 2024 at 2:51 pm #1470123Hi,
I did, of course, removed the PHP code.
Thanks for your excellent support.
SergeOctober 30, 2024 at 5:13 am #1470196 -
AuthorPosts
- The topic ‘Openi Accordion Tabs with Javascript’ is closed to new replies.