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 8834315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:59:04+00:00 2026-06-14T08:59:04+00:00

Basically the question is self explanitory. I can’t figure out how to combine small

  • 0

Basically the question is self explanitory. I can’t figure out how to combine small snippets of javascript rather than having two separate files.

The two codes are :

$(function(){
    $('.navbar li').each(function(){
      if(window.location.href.indexOf($(this).find('a:first').attr('href'))>-1)
        {
      $(this).addClass('active').siblings().removeClass('active');
        }
    });
});

$(function(){
    $('.dropdown-menu li').each(function(){
      if(window.location.href.indexOf($(this).find('a:first').attr('href'))>-1)
        {
      $(this).removeClass('active')
        }
    });
});

Another question, is the first $(function() necessary?

Thanks for you help.

  • 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-14T08:59:05+00:00Added an answer on June 14, 2026 at 8:59 am

    You can make it like this:

    $(function(){
        $('.navbar li, .dropdown-menu li').each(function(){
          if(window.location.href.indexOf($(this).find('a:first').attr('href'))>-1)
            {
                if($(this).parents(".navbar").length > 0)//this is to figure out if we are working with LI inside .navbar or not.
                    $(this).addClass('active').siblings().removeClass('active');
                else
                    $(this).removeClass('active')
    
            }
        });
    });
    

    Functions are almost equal, except that you need to detect if you are working with LI from .navbar or not to select corect removeClass code.

    Also, in your code first $(function() is necessary. Otherwise code inside it could be started before document is loaded. But you may put both pieces of code inside one $(function()

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

Sidebar

Related Questions

SHORT VERSION OF QUESTION: So basically my question is: How can I set the
I can provide more detail if necessary, but my question is basically thus: If
The title of this question should be self explanatory, but basically I have this
Basically the question is how do I run gpgpu code in a Metro app.
Basically my question is the exact same one as this: Simple client/server, TCP/IP encrypting
Basically the question could more probably be more accurately asked, how do I simply
Basically the question is, Why are NONE of the changes I've been making in
Basically my question is i have a website which was developed around 3 years
This is basically the same question as Click items in select box, and then
Today I stumbled upon this thread: http://www.mathworks.com/matlabcentral/newsreader/view_thread/112560 The question is basically how to make

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.