最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript to scroll <textarea> content back to top - Stack Overflow

programmeradmin3浏览0评论

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=".1.1/jquery.min.js"></script>
<button>Scroll</button>
<textarea Name="update" Id="update" cols="50" rows="25"></textarea>

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>

Share Improve this question edited Dec 24, 2017 at 6:58 Pedram 16.6k10 gold badges47 silver badges73 bronze badges asked Dec 24, 2017 at 6:44 Cody CodersonCody Coderson 4211 gold badge10 silver badges22 bronze badges 2
  • Scroll to top of jQuery-Textarea | Just change $textarea.scrollTop($textarea[0].scrollHeight); => $textarea.scrollTop(0); – Pedram Commented Dec 24, 2017 at 6:47
  • Do not put document.ready inside a click function! – Pedram Commented Dec 24, 2017 at 6:49
Add a comment  | 

4 Answers 4

Reset to default 12

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>

Try this javascript code. Set the top and left to 0 and scrolls the textarea to top from bottom.

function scrolltop() {
  var scr_top = document.getElementById("update");
  scr_top.scrollLeft = 0;
  scr_top.scrollTop = 0;
}
<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>
  <button onclick="scrolltop()">Scroll</button>
  <textarea name="update" id="update" cols="50" rows="25"></textarea>
</body>

</html>

Here's the complete code.

On button click, The content would navigate to bottom and/or top of the content of the tag

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="last">Scroll to bottom</button>
<textarea Name="update" Id="update" cols="50" rows="25"></textarea>

<script>
$(document).ready(function() {
  $("#last").on("click", function() {
    var $textarea = $('#update');
    $textarea.scrollTop($textarea[0].scrollHeight);
  });
});
</script>

<button id="first">Move to Top</button>

<script>
$(document).ready(function() {
  $("#first").on("click", function() {
    var $textarea = $('#update');
    $textarea.scrollTop(0);
  });
});
</script>

Two issues here:

  1. document.ready should be checked on the outer scope, before any usage of JQuery.
  2. for scrolling up to element top, scroll value should be 0

Hence:

  $(document).ready(function() {
    $("button").on("click", function() {
        var $textarea = $('#update');
        $textarea.scrollTop(0);
    });
  });
发布评论

评论列表(0)

  1. 暂无评论