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

  • Home
  • SEARCH
  • 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 7182425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:46:06+00:00 2026-05-28T17:46:06+00:00

How do you make this code shorter, i’m very new to jquery but i

  • 0

How do you make this code shorter, i’m very new to jquery but i managed to build what i wanted, but is there another way to do this e.g more nicely.

Thanks!

<script type="text/javascript"> 

       $(document).ready(function () {
       $(".genre").click(function () {
       $(".sobox").hide(); 
       $(".fobox").fadeIn() 
       $("#notcurrent").css({"background":"#9aa0ae", "color":"#fefefe"}); 
       $("#current").css({"background":"#2568be", "color":"#fff"}); 

         });

       $(".other").click(function () {
       $(".fobox").hide();  
       $(".sobox").fadeIn();  
       $("#current").css({"background":"#9aa0ae", "color":"#fefefe"}); 
       $("#notcurrent").css({"background":"#2568be", "color":"#fff"}); 

       });
      });

</script> 
  • 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-28T17:46:06+00:00Added an answer on May 28, 2026 at 5:46 pm

    I don’t see any serious issues with your existing code, but you could store the CSS style objects in variables rather than repeating them.

    Additionally, so long as all the elements exist in the DOM at the time your click event handlers are defined, you could use cached jQuery selectors for a small performance gain.

    i.e. assuming in your first event handler you meant to change the style of the #notcurrent element and not a different one #ncurrent, and that the fobox/sobox/current/notcurrent elements exist when the DOM is initially loaded:

    <script type="text/javascript"> 
    
    $(document).ready(function () {
      var styleA = {"background":"#9aa0ae", "color":"#fefefe"},
          styleB = {"background":"#2568be", "color":"#fff"},
          current = $("#current"),
          notCurrent = $("#notcurrent"),
          fobox = $(".fobox"),
          sobox = $(".sobox");
    
      $(".genre").click(function () {
        sobox.hide(); 
        fobox.fadeIn() 
        notCurrent.css(styleA); 
        current.css(styleB);
      });
    
      $(".other").click(function () {
        fobox.hide();  
        sobox.fadeIn();  
        current.css(styleA); 
        notCurrent.css(styleB); 
      });
    });
    
    </script>
    

    Long story short, I don’t see a clear way to significantly reduce the size of the code, but there are some ways you could make it a little more efficient while maintaining readability. Other answers show how you could define a single callback function that may result in less code.

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

Sidebar

Related Questions

Is there any way to make this code shorter? long call_f(int argc, long *argv)
Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String
MyClass c = 10; Is there any way to make this code work? I
Is there anyway I can make this database code any shorter ? It works
How to make this css code cross browser compatible using jquery. this code only
If there is any possibility to make this code simpler, I'd really appreciate it!
i've tried using this code and this to make a random quote generator, but
How to merge these (make this code shorter)? $match_1 = false; $match_2 = false;
I am new to javascript.Itried to make this page but somehow shorten function does
Can I make this code shorter than this ? - (void) setupFeature { NSArray

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.