0

I'm having a set of tables that hide and show content when clicked on their th. Also I'm using scrollTop to reach to the top of each clicked table to better show its content. No problem so far... By default, I want the first table to show its content but I want to prevent the scrollTop in this case when the page loads. How can I do it?

$(document).ready(function() {

$('table tr').nextAll().hide();
var $button = $('table th').css('cursor', 'pointer');
$button.click( function() {
$(this).parent().nextUntil('table th').toggle();
$('html, body').animate({
    scrollTop: $(this).offset().top
    }, 500);
});  
$('table th:eq(0)').trigger('click', animate(false));
});

There must be some piece of code on .trigger() that helps me prevent this first scroll? Thanks for your help!

EDIT: Well, not the most elegant of solutions but splitting the events and having the trigger() placed before the animations seemed to work:

$(document).ready(function() {

$('table tr').nextAll().hide();
var $button = $('table th').css('cursor', 'pointer');
$button.click( function() {
        $(this).parent().nextUntil('table th').toggle();
}); 

$('table th:eq(0)').trigger('click');

$button.click( function() {
        $('html, body').animate({
        scrollTop: $(this).offset().top
        }, 500);
}); 
});
1

1 Answer 1

1

Try moving the animation code inside the click handler, i.e. remove the animate code, and change your trigger code to be like this:

$('table th:eq(0)').click(function() {
    $('html, body').animate({
        scrollTop: $(this).offset().top
        }, 500);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! Well, the animations are working ok when I click the TH, but on load I want "$('table th:eq(0)').click()" not to scroll given that there is not actual input form the user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.