0

I have a form with multiple input fields, such that before I get to the submit button I would have scrolled to the bottom of the page. I use jquery to submit the form without refreshing the page. When the form submits, the submit button area, i.e the bottom of the form is still being displayed. How can I adjust my code so that once the form is submitted, it returns to the top of the page without refreshing the page? My code is shown below.

Jquery

$("#sub").click( function() {
var content =   tinyMCE.activeEditor.getContent();
$('textarea[name=texteditor]').val(content);
$.post( $("#myform2").attr("action"), $("#myform2").serialize(), function(info){ $("#result").html(info); } );
clearInput();
});

$("#myform2").submit( function() {
    return false;
});

function clearInput() {

$("#myform2")[0].reset(); 
}
Oponjous
  • 37
  • 7
  • Possible duplicate of [Scroll to the top of the page using JavaScript/jQuery?](https://stackoverflow.com/questions/1144805/scroll-to-the-top-of-the-page-using-javascript-jquery) – Obsidian Age Aug 27 '17 at 22:18

3 Answers3

1

You can use this code to scroll smoothly to the top of the page

$("html, body").animate({ scrollTop: 0 }, "slow");
Chris
  • 4,672
  • 13
  • 52
  • 93
0

Use

<a href="#">link</a>

to anchor back to the top.

Edit: For JS, see this page:

How to scroll to top of page with JavaScript/jQuery?

jonathan.ihm
  • 108
  • 1
  • 1
  • 8
0

I'll try and help! :D

HTML here, make sure to add an ID to the body so we can go to the top!

<body id="top">
    <!-- content... -->
</body>

Next, the javascript!

// This will take the user to the top of the page.
// I haven't tested this, so it may or may not work, but you get the idea.

$('#form').submit(function() {
    window.location = window.location + "#top";
});