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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:08:37+00:00 2026-05-27T02:08:37+00:00

I have some jQuery code that I would like reviews and pointers on on

  • 0

I have some jQuery code that I would like reviews and pointers on on how to bring its line count down and shorten.

$('#p1').click(function() {
    $('#list').fadeOut(450);
    $('#q1').delay(600).fadeIn(450)
});
$('#p2').click(function() {
    $('#list').fadeOut(450);
    $('#q2').delay(600).fadeIn(450)
});
$('#p3').click(function() {
    $('#list').fadeOut(450);
    $('#q3').delay(600).fadeIn(450)
});
$('#p4').click(function() {
    $('#list').fadeOut(450);
    $('#q4').delay(600).fadeIn(450)
});

...

$('#p12').click(function() {
    $('#list').fadeOut(450);
    $('#q12').delay(600).fadeIn(450)
});
$('#p13').click(function() {
    $('#list').fadeOut(450);
    $('#q13').delay(600).fadeIn(450)
});

Can this code be better optimised? Or at least made less verbose?

  • 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-27T02:08:37+00:00Added an answer on May 27, 2026 at 2:08 am

    You can use a for loop, but you should make sure the loop counter’s value gets into a correct scope for click event handler:

    var clickHandler = function(k) {
        return function() {
            $('#list').fadeOut(450);
            $('#q' + k).delay(600).fadeIn(450);
        };
    };
    for (var i = 1; i < 14; ++i) {
        $('#p' + i).click(clickHandler(i));
    }
    

    Otherwise the delay and fadeIn would get applied to #q13 element exclusively, since actual counter (with its final value of 13) would get into closure.


    EDIT: Since quite a lot of answers got it wrong here, I’ll attempt to explain more precisely what’s going on in this code, as it seems to be pretty confusing.

    The “natural” solution with injecting the click handler directly into loop would be the following:

    for(var i = 1; i < 14; i++) {
        $('#p'+i).click(function() {
            $('#list').fadeOut(450);
            $('#q'+i).delay(600).fadeIn(450)
        });
    }
    

    But this is not at all equivalent to the extended form, which lists all the 13 variants one after another. The problem is that while there are indeed 13 functions created here, they are all closed over the same variable i, whose value changes. It finally arrives at the value of 13 and the loop ends.

    Some time later the functions attached to #p1…#p13 elements are called (when one of those elements are clicked) and they use that final value of i. This results in only #q13 being animated.

    What needs to be done here is to do something called lambda lifting and eliminate the free variable i, whose value gets inadvertly changed. A common technique for that is to provide a “factory function” which accepts value for our variable and outputs an actual function which we’ll use as event handler:

    var clickHandler = function(k) {
        return function() {
            $('#list').fadeOut(450);
            $('#q' + k).delay(600).fadeIn(450);
        };
    };
    

    Since the scope of k parameter is local to clickHandler, every call to clickHandler gets different k variable. The function returned from clickHandler is therefore closed over different variables, which can in turn have different values. This is exactly what we need. We can then call clickHandler from our loop, passing it the counter’s value:

    for (var i = 1; i < 14; ++i) {
        $('#p' + i).click(clickHandler(i));
    }
    

    I hope this makes the difference somewhat clearer.


    EDIT: As Esailija pointed out in the comments, it is also possible to use jQuery.each to achieve similar effect:

    $.each(new Array(13), function(idx) {
        $('#p' + (idx + 1)).click(function() {
            $('#list').fadeOut(450);
            $('#q' + idx).delay(600).fadeIn(450);
        });
    });
    

    This is probably the solution of choice if you’re already aware of the closure/scoping issue I’ve tried to outline above.

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

Sidebar

Related Questions

I have some jquery code that is doing an ajax lookup and returning comma
I have some jQuery code that highlights a link when clicked, and changes the
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have some jQuery/JavaScript code that I want to run only when there is
I have some jQuery code. I have called an Ajax function file, file.php, that
Trying to get comfortable with jQuery and I have encountered some sample code that
I have some jQuery code something like this: $(document).ready(function() { $(img.off).click(function() { alert('on'); $(this).attr('class',
Hey guys, I have some code that I found that I would rather use
I have some (potentially) long-running ajax calls that I would like to abort if
Hi I have the following jQuery code that splits some content after the second

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.