- This topic has 17 replies, 2 voices, and was last updated 3 years, 8 months ago by Leo.
-
AuthorPosts
-
February 17, 2017 at 6:48 am #280156FIRST
I know there are a few plugins to create the accordion feature on pages, however I guess I’d like to avoid using another plugin if possible. I’m trying to recreate the following on one of my pages: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_accordion_symbol
I’ve put the main text in the ‘text’ area, I put the css portion in the ‘simple css’ section and I put the javascript in the GP hooks section under wp_footer (and checked enable PHP). Unfortunately, it’s still not working. Am I missing something very simple?
Thanks!
February 17, 2017 at 6:57 am #280160jmarcHum … give a try with “shortcodes ultimate” for example … 🙂
February 17, 2017 at 8:36 am #280225LeoStaffCustomer SupportYup lots of plugins options out there. Here are a few:
https://en-ca.wordpress.org/plugins/shortcodes-ultimate/
https://en-ca.wordpress.org/plugins/accordion-shortcodes/
https://en-ca.wordpress.org/plugins/accordions/February 17, 2017 at 9:01 am #280236FIRSTThanks. I am aware of the plugins, but this seems like a relatively simple thing that can be done using html/css/javascript. Should I be putting the javascript in another space?
Thanks again
February 17, 2017 at 9:03 am #280237LeoStaffCustomer SupportTry adding it using one of these methods: https://docs.generatepress.com/article/adding-php/
Let me know.
February 17, 2017 at 9:43 am #280267TomLead DeveloperLead DeveloperJavascript can be put in wp_footer if it’s wrapped in
<script></script>
tags.February 17, 2017 at 1:04 pm #280345FIRSTThanks, Tom. That’s what I thought. I put the javascript code provided here https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_accordion_symbol directly into the wp_footer section in GP Hooks. It sort of works… The plus changes to minus, but the text doesn’t drop down. Very weird.
February 17, 2017 at 2:42 pm #280375LeoStaffCustomer SupportWithout seeing your site is hard for us to tell what’s going on.
Make sure everything is added from that page you linked. The demo they show is running off the code on the left side so if you add the exact same code the result should be the same.
February 21, 2017 at 10:13 am #282013FIRSTThe page is http://bidmcfirst.com/test/. I’ve added the following code in the wp_footer section under GP hooks:
<?php if ( is_page( 'test' ) ) : ?> <script> var acc = document.getElementsByClassName("accordion"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function() { this.classList.toggle("active"); var panel = this.nextElementSibling; if (panel.style.maxHeight){ panel.style.maxHeight = null; } else { panel.style.maxHeight = panel.scrollHeight + "px"; } } } </script> <?php endif; ?>
I’m getting the following error:
Uncaught TypeError: Cannot read property 'style' of null at HTMLButtonElement.acc.(anonymous function).onclick
February 21, 2017 at 8:00 pm #282207TomLead DeveloperLead DeveloperIt says that page isn’t found.
February 22, 2017 at 4:20 am #282360FIRSTSorry about that. I had set it to private accidentally. Should be all set now.
February 22, 2017 at 12:06 pm #282600TomLead DeveloperLead DeveloperHmm, not sure that JS is the best method.
Try this: http://uniondesign.ca/simple-accordion-without-jquery-ui/
March 6, 2017 at 3:31 am #287654ssmatgpI tried the code you, Tom, referred to in your reply on February 22, 2017, but the actual accordion effect is missing. In the following, I document what I did, i.e., copied and pasted. Please tell me where I made a mistake. In step 3, I give the link to the resulting page.
As an alternative, I tried the code from http://www.w3schools.com/howto/howto_js_accordion.asp, which did not work either.
Thanks for your assistance.
(1) Inserted the following in “GP Hooks > wp_footer”
<script type=”text/javascript”>
$(document).ready(function($) {
$(‘#accordion’).find(‘.accordion-toggle’).click(function(){//Expand or collapse this panel
$(this).next().slideToggle(‘fast’);//Hide the other panels
$(“.accordion-content”).not($(this).next()).slideUp(‘fast’);});
});
</script>(2) Inserted the following in “Customizer > Custom/Additional CSS”
.accordion-toggle {cursor: pointer;}
.accordion-content {display: none;}
.accordion-content.default {display: block;}(3) Inserted the following on the page http://www.schulte-martini.de/accordiontest1/
<h4 class=”accordion-toggle”>Accordion 1</h4><p>Cras malesuada ultrices augue molestie risus.</p><h4 class=”accordion-toggle”>Accordion 2</h4>
<p>Lorem ipsum dolor sit amet mauris eu turpis.</p><h4 class=”accordion-toggle”>Accordion 3</h4>
<p>Vivamus facilisisnibh scelerisque laoreet.</p>March 6, 2017 at 9:35 am #287793TomLead DeveloperLead DeveloperYour JS should look like this to work with WP:
<script type="text/javascript"> jQuery(document).ready(function($) { $('#accordion').find('.accordion-toggle').click(function(){ //Expand or collapse this panel $(this).next().slideToggle('fast'); //Hide the other panels $(".accordion-content").not($(this).next()).slideUp('fast'); }); }); </script>
March 7, 2017 at 2:56 pm #288512ssmatgpThank you for your quick assistance, Tom.
Now it is running; but only after I excluded jquery.js from being deferred by Async Javascript, a plugin you recommend for optimization. Perhaps you should update your recommended settings of that plugin.
-
AuthorPosts
- The topic ‘simple accordion feature’ is closed to new replies.