0

This is a extension to my previous question How to fix a div height.

Thank you all for helping me to fix the size.

But after this I came across my last issue. i.e. scrolling.

when the user enters message and hits an enter, a two new divs are getting added. one with the user data and the other with the response data.

The data is generated correctly, but in the chat window, instead of showing the last div, it is showing the previous div.

Current Output enter image description here

Expected Output

enter image description here

I visited some SO forums, but none of them did the trick.

Please let me know how can I fix this.

Thanks

Community
  • 1
  • 1
user3872094
  • 3,269
  • 8
  • 33
  • 71

2 Answers2

1

You can adapt this code to your situation: [your_element].scrollTop = [your_element].scrollHeight

For more information you can check this question: Scroll Automatically to the Bottom of the Page

Community
  • 1
  • 1
mickaelw
  • 1,453
  • 2
  • 11
  • 28
1

Each time a new div is added to the chat window, you will need to scroll the div using JavaScript or jQuery.

something like:

For using jQuery:

$('#chatHistory').scrollTop($('#chatHistory').scrollHeight);

For plain JavaScript:

elmnt = document.getElementById("chatHistory");
elmnt.scrollTop = elmnt.scrollHeight;

This is similar to: Use jQuery to scroll to the bottom of a div with lots of text

Community
  • 1
  • 1
Andrew Stalker
  • 718
  • 5
  • 22