How to Scroll to the top of the page using JavaScript

0 votes
How do I scroll to the top of the page using a JavaScript? It is desirable even if the scrollbar instantly jumps to the top. I'm not looking for a smooth scrolling.

Thank You!!
Apr 2, 2020 in Java-Script by kartik
• 37,530 points
189 views

1 answer to this question.

0 votes

Hii @kartik,

Using javascript

<script>

  $("a[href='#top']").click(function() {
     $("html, body").animate({ scrollTop: 0 }, "slow");
     return false;
  });
</script>

However you don't need javascript  to do this. A standard HTML tag will suffice..

<a href="#top">go top</a>

Thank You!!

answered Apr 2, 2020 by Niroj
• 82,820 points

Related Questions In Java-Script

0 votes
2 answers

How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?

For security reasons browsers do not allow ...READ MORE

answered Dec 16, 2020 in Java-Script by Rajiv
• 8,890 points
27,792 views
0 votes
1 answer

How to get the file name from a full path using JavaScript?

Hello @kartik, Use this: var filename = fullPath.replace(/^.*[\\\/]/, '') This ...READ MORE

answered Sep 2, 2020 in Java-Script by Niroj
• 82,820 points
274 views
0 votes
1 answer

How can I make a div stick to the top of the screen once it's been scrolled to?

Hello @kartik, Using javascript: var initTopPosition= $('#myElementToStick').offset().top; ...READ MORE

answered Sep 4, 2020 in Java-Script by Niroj
• 82,820 points
124 views
0 votes
1 answer

How to get the size of a JavaScript object?

Hello @kartik, Use this code: function roughSizeOfObject( object ) ...READ MORE

answered Sep 23, 2020 in Java-Script by Niroj
• 82,820 points
680 views
0 votes
1 answer

Why it is necessary to refresh CSRF token per form request?

Hello, Generating a new CSRF token for each ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,820 points
2,192 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,820 points
739 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,820 points
234 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,820 points
726 views
0 votes
1 answer

How to get the host url using javascript from the current page?

Hello @kartik, Use: var host = window.location.hostname; or possibly var host ...READ MORE

answered Oct 9, 2020 in Java-Script by Niroj
• 82,820 points
293 views
0 votes
1 answer

How to list the properties of a JavaScript object?

Hii @kartik, Use Reflect.ownKeys(): var obj = {a: 1, b: ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,820 points
210 views