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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:31:32+00:00 2026-06-02T17:31:32+00:00

This question has come up a lot but I can’t make it work. I

  • 0

This question has come up a lot but I can’t make it work.

I have 5 divs, I want to hide the div before the last one who was used. So If the user clicks somewhere in div 1, and then clicks somewhere in div 2, div 1 fades out (all of this after an Ajax call)

Here’s my code:

$(document).ready(function() {
    $("#[id^='r_form_']").bind("ajax:success", function(evt, data, status, xhr) {
        var $form = $(this);

        $(this).parent().css("background", "green");

        if($lastForm == null) {
            var $lastForm = $(this);
        };

        if(!($lastForm[0] == $form[0])) {
            $lastForm.parent().fadeOut(1500);
            var $lastForm = $(this);
        };
    });
});

If the variable $lastForm is undefined, assign the current form where Ajax happenned.

The variable is always undefined. I added an alert('undefined') in the loop, i always get it. why?

I have a feeling it might be because the variable is reset when the Ajax request comes back. But i’m not really an expert and can’t seem to find the answer.

  • 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-02T17:31:34+00:00Added an answer on June 2, 2026 at 5:31 pm

    I think it’s because it’s declared in inner scope. move the declaration to outside of the function. each function create a new scope, so it’s not saved to the next call:

    Code:

    $(document).ready(function() {
    
        var $lastForm; //<===================
        $("#[id^='r_form_']").bind("ajax:success", function(evt, data, status, xhr) {
            var $form = $(this);
    
            $(this).parent().css("background", "green");
    
            if ($lastForm == null) {
                $lastForm = $(this);
            };
    
            if (!($lastForm[0] == $form[0])) {
                $lastForm.parent().fadeOut(1500);
                $lastForm = $(this);
            };
        });
    });​
    

    As commented by @minitech the # in the selector: #[id^='r_form_'] looks like it shouldn’t be there.

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

Sidebar

Related Questions

This question has been asked before ( link ) but I have slightly different
This question has been puzzling me for a long time now. I come from
This question has been brought up before, and I have searched many of the
This question has been asked before but with no answer. I'm running into brick
This question has been asked in a C++ context but I'm curious about Java.
I've seen this problem come up a lot, but never adequately handled, and I
I suspect this question has been asked before, but it's not easy to Google
I know this question has gone round a couple of times now but the
This question has been asked before at How does Android enforce permissions? . While
This question has been discussed in two blog posts ( http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/ , http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/ ),

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.