Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to scroll <textarea> content back to top

I want the following code, changed so that on button press, it scrolls the text area content from bottom to top.

$("button").on("click", function() {
  $(document).ready(function() {
    var $textarea = $('#update');
    $textarea.scrollTop($textarea[0].scrollHeight);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Scroll</button>
<textarea Name="update" Id="update" cols="50" rows="25"></textarea>
like image 888
Cody Coderson Avatar asked Dec 24 '17 06:12

Cody Coderson


People also ask

How to scroll back to the top of scrollable div with JavaScript?

To scroll back to the top of scrollable div with JavaScript, we can set the scrollTop property of the scrollable div to 0. const myDiv = document.getElementById ("containerDiv"); //... myDiv.scrollTop = 0;

How to go to top using JavaScript scrolltop without library?

You can call this a Pure HTML CSS & JavaScript Go To Top function, without any library. Basically, the whole program is based on JavaScript scrollTop command. And the button is fully styled in CSS. Now if you are thinking about how this JavaScript scrollTop program actually is? then see the preview given below.

How to go to top of the web page?

Solution: JavaScript Scroll To Top Feature, HTML CSS JavaScript Go To Top button. Nowadays every blog and websites have a sperate button for going back to the top of the webpage. When we read or watch content on website then we start scrolling down to see more, but many times we want to go back on the upper side.

Should you add a scroll button to your web page?

If the web page contains detailed content then including a back-to-top scroll button is strongly suggested. A button to get back to the topmost of the web page permits the end users to rapidly get back to the start of the web page without putting forth a lot of attempts.


1 Answers

Change the following line $textarea.scrollTop($textarea[0].scrollHeight); to $textarea.scrollTop(0); to scroll to the top of textarea.

$("button").on("click", function() {
    var $textarea = $('#update');
    $textarea.scrollTop(0);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Scroll</button>
<textarea Name="update" Id="update" cols="50" rows="25"></textarea>
like image 116
cdoshi Avatar answered Oct 19 '22 00:10

cdoshi