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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:20:23+00:00 2026-05-21T18:20:23+00:00

I know this is a popular topic and I’ve read some other articles on

  • 0

I know this is a popular topic and I’ve read some other articles on substring selection in jQuery, but can’t figure out how to get my specific example working more efficiency and any advice would be appreciated.

I have the following code:

$('#plan-gallery').click(function(){
    $('#gallery').addClass('active-slide') 
    $('#gallery').fadeIn() 
});

$('#plan-ensuite').click(function(){
    $('#ensuite').addClass('active-slide') 
    $('#ensuite').fadeIn() 
});

$('#plan-bedroom').click(function(){
    $('#bedroom').addClass('active-slide') /* This code line is redundant */
    $('#bedroom').fadeIn() /* This code line is also redundant */
});

This kind of code is repeated many times throughout my script.js and I’d like to get rid of it’s redundancy.

Can any jQuery wizards help make my code better?

Thanks in advance

  • 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-21T18:20:24+00:00Added an answer on May 21, 2026 at 6:20 pm

    Assuming that the id of the slide starts after the first - in the slide button id:

    $('#plan-gallery,#plan-ensuite,#plan-bedroom').click(function(){
       $('#' + this.id.substring(this.id.indexOf('-') + 1)).addClass('active-slide').fadeIn();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this might be a no-brainer, but please read on. I also know
I know this question is overly popular, but I find nothing that addresses rewriting
i know, this is a realy popular error. But my problem is that my
I know this won't be a popular question, because a lot of web designers
I know this isn't strictly a programming question but y'all must have experienced this.
I know this is a broad question, but I've inherited several poor performers and
I know this is not programming directly, but it's regarding a development workstation I'm
I know this rather goes against the idea of enums, but is it possible
I know this would be easy with position:fixed, but unfortanately I'm stuck with supporting
I know this is a dumb question. For some reason my mind is blank

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.