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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:01:37+00:00 2026-05-26T12:01:37+00:00

Im trying to cut down some code, basically the following click event is used

  • 0

Im trying to cut down some code, basically the following click event is used 10 times with the id ranging from #topNavA – #topNavJ, there’s only 2 parts of the event that ever changes. Can anyone suggest how I can recycle this code for each click? I’ve added in comments of the lines that will change from click event to click event but the rest just stays the same? All advice welcome.

$('#topNavA').click(function() {
    $footerPush.hide();
    $allMultipleElements.hide();

    $('#section, #placement, .mobileControls').show(); // This line will change with different selectors
    $('.mobileControls h1').replaceWith('<h1>Mobilizing Mobile, AL</h1>'); // This line will change with a different header 1
    $('.mobileControls h2').replaceWith('<h2>Overview</h2>'); // This line will change with a different header 2
    $('.menuTitle').removeClass('active');
    $('.lightbox').remove();

    $('#nav li > a').removeClass('active');
    $('#nav li span a').css({
        'color': '#8F8F8F'
    });

    $('#topNav').addClass('active');
    $(this).css({
        'color': '#e8af20'
    });
});

Thanks
Kyle

  • 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-05-26T12:01:37+00:00Added an answer on May 26, 2026 at 12:01 pm

    One option would be to use data. You could have 3 data- attributes on the elements which store the variable data. Something like this:

    <a href="..." id="topNavA" class="topNavLink" data-selector="#section, #placement, .mobileControls">something</a>
    

    $(".topNavLink").click(function(){
        var selector = $(this).data("selector");
    
        $(selector).dosomething();
    });
    

    And example of the technique: http://jsfiddle.net/mPQfJ/1/

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

Sidebar

Related Questions

I am trying to tidy up some legacy code. The following is a cut
Im on an optimization crusade for one of my sites, trying to cut down
I am trying to cut down jQuery to a very specific set of functions
I'm trying to find ways to cut down memory usage for my app right
I'm trying to take a {variable} string and cut it down for entry into
so here's a cut down version of my code: class lookAClass { public $pageTitle;
I am trying to cut over an existing WebForms app to use Routing, and
I'm trying to use Unix cut to remove the first two fields per line.
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to find some simple SQL Server PIVOT examples. Most of the examples that

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.