Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:13:22+00:00 2026-06-10T03:13:22+00:00

I have a simple nav menu with submenus. When the links in the submenu

  • 0

I have a simple nav menu with submenus. When the links in the submenu are clicked while on the same page, the window scrolls using jQuery to the respective anchor.

Here is an example of a submenu list item:

<li><a href="page.html#some-anchor" onclick="clickScroll('#some-anchor');"><span>foo</span></a></li>

The corresponding anchor looks like this:

<a class="hidden-anchor" id="some-anchor" name="some-anchor"></a>

The JavaScript function:

function clickScroll(dest) {
    $('html, body').stop().animate({
        scrollTop: $(dest).offset().top
    }, 1000);
}

It all works perfectly fine, but like many before me I have been reading that inline event handlers are bad practice these days.

How can I modify the function to eliminate the need for any onclick calls? Keep in mind that each submenu link corresponds to a different anchor.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:13:23+00:00Added an answer on June 10, 2026 at 3:13 am

    Assuming that you want all anchors with a hash # to scroll to a corresponding anchor with the same ID, you can loop through all anchors, parse out the hash and scroll to the same ID:

    $('a').each(function() {
        if ( this.hash ) {
            $(this).click(function(e) {
                $('html,body').animate({ scrollTop: $(this.hash).offset().top }, 1000);
                e.preventDefault();
            });
         }
    });
    

    http://jsfiddle.net/nGfW5/

    If you want to limit this functionality to certain anchors, add a class and insert it in the selector, f.ex $('a.hashlink').each(

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple jQuery click and scrollTO anchor script. I have another page,
I have a simple nav menu: <ul> <li><a href=index.php id=home_menu>Home</a></li> <li><a href=purchase.php class=selected id=information_menu>Information</a></li>
I have a simple navigation menu with plain html links that target different pages.
I have a simple drop-down menu that is controlled using the following script: $(document).ready(function
I have a simple test page I'm using to test out displaying partial views
I have the following simple 3 level menu structure: <nav class=main-nav class=list> <ul> <li
I have a simple horizontal nav menu that uses highly-styled anchors for buttons. Now,
I have a simple nav made up of the following HTML; <div class=inner> <div
Ok I have a inline list of buttons. <ul id=nav> <li class=home><a href=#>Menu Item</a></li>
My goals: I want to have #nav to be divide into menu-left and menu-right,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.