Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #97047

    JCastelize
    Participant

    Is there a way to place a button inside the accordion that closes it when you’re done reading? I have a lot of text in my accordions so by the time you’re done reading it, you have to scroll a ways up to close it again.

    All X-theme shortcodes and wordpress are up to date. Here’s the page to which the question relates: http://www.wpgelsite.info/TriCore/testimonials/

    #97154

    Zeshan
    Member

    Hi Jacqueline!

    Thank you for writing in!

    To achieve this, add the following jQuery code under Custom > JavaScript in the Customizer:

    jQuery(document).ready(function($) {
       var close = $('.accordion-close'),
           accBody = close.closest('.accordion-body'),
           accHead = accBody.siblings('.x-accordion-heading').find('.x-accordion-toggle');
       
       close.click(function(e) {
          e.preventDefault();
          accBody.removeClass('in').animate({'height': '0px'}, 300);
          accHead.addClass('collapsed');
       });
    
    });
    

    And add a link with class accordion-close at the end of you accordion body, like:

    <a href="#" class="accordion-close">Close</a>
    

    Hope this helps. 🙂

    Thank you.

    #97925

    JCastelize
    Participant

    Thanks! That did the trick.

    #97932

    Kosher K
    Member

    Good to hear that.

    Cheers

    #236698

    Misho
    Participant

    Hi!

    I have just used this trick, works great!

    I have added one more functionality:

    If you close the long accordion with a button or link like this, you will end up at the place where the accordion ended, and you may have some content above which you have missed, so you need to scroll up. The user may not even realize that he has just missed some content and continue to read from the place he ended up.

    To solve this, simply add an ID to the accordion, like my-accordion.

    Then modify the above link into: <a class="accordion-close" href="#my-accordion">Close</a>

    This will make the page scroll back to where you have started, so you can continue down the page after finishing with the accordion.

    I hope this will be helpful. 🙂

    Take care!

    #236848

    Rue Nel
    Moderator

    Hello Misho,

    Thanks for sharing what have you done in your site. For sure other X theme users will find this very handy.

    Cheers,
    X

    #237089

    Misho
    Participant

    No problem! 🙂

    Unfortunately, i have found a downside on the second method: it will not work on touch screens. When pressed on a touch screen, it will not close the accordion, it will only make scroll to it.

    When clicked with the mouse, both actions are executed, but when touched – only one works.

    If there is no fix for this, the workaround is to make it visible for desktop only.

    #237101

    Thai
    Moderator

    Hi @misho,
    Please try this following code instead of the previous code:

    
    jQuery(document).ready(function($) {
    	var close = $('.accordion-close'),
    	accBody = close.closest('.accordion-body'),
    	accHead = accBody.siblings('.x-accordion-heading').find('.x-accordion-toggle');
    
    	close.on('click touchend',function(e) {
    		e.preventDefault();
    		accBody.removeClass('in').animate({'height': '0px'}, 300);
    		accHead.addClass('collapsed');
    	});
    });
    

    Hope it helps.

    #237914

    Misho
    Participant

    Works on all devices now!

    X support = wizards of code. 🙂

    #237917

    Paul R
    Moderator

    Thank you for your kind words. 🙂

    Let us know if you have any further questions.

    #725515

    Stjarne
    Participant

    When i implent the javascript and the <a class= etc in my accordian i get the close button, but when i click it i get scrolled up to where the accordian starts but it does not close the accordian… ?

    #725527

    Rupok
    Member

    Hi @stjarne

    Thanks for updating. Would you please share your URL and point us where you are trying this? Also paste the code you have added.

    Cheers!

    #725594

    Stjarne
    Participant
    This reply has been marked as private.
    #725804

    Nabeel A
    Moderator

    Hi again,

    Thanks you for providing the credentials, there was a syntax error in your jQuery script. I went ahead and fixed it for you. Now the close button works fine. Also I modified your code to close previous accordions when opening another.

    Clear your browser’s cache and check the site again. Cheers!

    #725845

    Stjarne
    Participant

    Awsome!!!!

    Cheers