1

I know this is by far not the first time this question is asked but I did look through every discussion I could found and none of the approaches was helpful to me (I'll list those solution-attempts further below). My problems is a Show More/Hide Button-Javascript that only seems to work after the first refresh of the page (interestingly, this only shows up if a non-admin user visits the site - if that makes a difference). My Code:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
   var SHOW_MORE = 'Show More'
   var COLLAPSE = 'Show Less'
   $(window).load (function(){
     $('a[href^="#expand"]').each(function(){
       var n = parseInt($(this).attr('href').split('-')[1]);
       var next_n_divs = $(this).parents('div.sqs-block').nextAll().slice(0,n)
       next_n_divs.wrapAll('<div class="extra-gallery" style="display:none;"></div>');
       $(this).click(function(){
         var target_gallery = $(this).parents('div.sqs-block').next('div.extra-gallery')
         if (target_gallery.is(':visible')){
           $(this).text(SHOW_MORE);
         }
         else {
           $(this).text(COLLAPSE);
         }
         target_gallery.slideToggle();
         return false;
       });
       });
   });
 </script>

I figured out it might be helpful to add a

$(window).load( function() {
    // ... 
});

but this only seemed to 'crash' the page (maybe I applied it wrong tho).

(Here the links I already took a look at: Javascript only works after page refresh Rails javascript only works after reload Rails javascript only works after reload ) thx for any help Titus

Community
  • 1
  • 1
Titus
  • 11
  • 2
  • Just `$(function () { ... });` would be more appropriate in this case, but neither will crash the page. You'll have to narrow the code down, removing chunks until you figure out exactly what is causing that.' – Brad Nov 20 '16 at 08:17
  • Nothing shown here should crash the page unless there are syntax errors (easily found in browser console). Create a live demo that reproduces the problem – charlietfl Nov 20 '16 at 08:19
  • 6
    Please stop adding code snippets just to show us your code. Code snippets are for to run whole code examples, not just to have your code formatted. – Christos Lytras Nov 20 '16 at 08:20
  • 1
    Also, please sure all the necessary code to reproduce the problem. In this case it would be helpful to see your HTML code. – Brett DeWoody Nov 20 '16 at 09:21
  • *Actually*, it would be especially nice if you *did* use a code snippet for a proper [mcve]. This should include everything necessary to reproduce the issue, with as little unnecessary code as possible. –  Nov 20 '16 at 09:35

0 Answers0