0

I have a div whose id is 'regOrg' and initially it is hidden using a css

display: none;

The link to be clicked has an id of back. On clicking a link I want it to show and smooth scroll down to it (the div regOrg).

This is the code i have:

function goBack() {
    $("#regOrg").show();
    $("#back").click(function() {
        $('html, body').animate({
            scrollBottom: $("#regOrg").offset().bottom
        }, 2000);
    });
}

I call the function goback() on the onclick event of the hyperlink. However it doesn't work. It only shows the div but doesn't scroll to it.

erikvimz
  • 5,256
  • 6
  • 44
  • 60
Andrea Tand
  • 91
  • 1
  • 2
  • 14
  • Possible duplicate of [How can I scroll to a specific location on the page using jquery?](http://stackoverflow.com/questions/1586341/how-can-i-scroll-to-a-specific-location-on-the-page-using-jquery) – ebram khalil Jun 16 '16 at 10:08

2 Answers2

0

You are not executing the scroll. You are just binding it to the click event.

Try:

function goBack() {
  $("#regOrg").show();
  $('html, body').animate({
     scrollBottom: $("#regOrg").offset().bottom + 'px'},2000);
}

EDIT: I added the missing "px" to it.

TehSphinX
  • 6,536
  • 1
  • 24
  • 34
0

Try with something like this :

function goBack() {
  $("#regOrg").show();
  var myDiv = $('#regOrg');
  var height = myDiv[0].scrollHeight;
  myDiv.scrollTop(height);
}

It works on jsfiddle You could take a look at this post too.

Community
  • 1
  • 1
kawai
  • 27
  • 8