0

I'm trying to hide and show paragraph's on a FAQ page. The paragraphs are hidden initially, and I add the following code:

  $(document).ready(function() {
    $('h3').click(function() {$(this).next("p").toggleClass("unhidden-paragraph") } )
  })

unhidden-paragraph styles it to show the paragraph. So if one clicks on a question (h3) then it shows the paragraph.

This works perfectly, however when I go to a different page on squarespace and then use the back button to my FAQ page. Then the ready is not called again and clicking on the h3 questions no longer works.

esqew
  • 42,425
  • 27
  • 92
  • 132
Otto
  • 663
  • 3
  • 17
  • 33
  • Possible duplicate of [history.back(); doesn't trigger $(document).ready();](https://stackoverflow.com/questions/3628536/history-back-doesnt-trigger-document-ready) – Mohammad Oct 10 '18 at 11:02
  • @Mohammad that question has no solutions that actually work. Or at least with squarespace. – Otto Oct 10 '18 at 19:55

1 Answers1

0

To execute some jQuery setup code even when the user enters the page from the back button, you can use $(window).on('pageshow') instead of $(document).ready():

$(window).on('pageshow', function(){
    $('h3').click(function() {$(this).next("p").toggleClass("unhidden-paragraph") } );
});

For "single-page" designs (a la Squarespace), you might have to just attach the method to the onclick attribute of the HTML element directly:

<h3 onclick='$(this).next("p").toggleClass("unhidden-paragraph");'>Title</h3>
esqew
  • 42,425
  • 27
  • 92
  • 132