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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:38:54+00:00 2026-05-25T02:38:54+00:00

For some reason my call to nested jQuery.each() functions are losing scope for some

  • 0

For some reason my call to nested jQuery.each() functions are losing scope for some variables, but not others. In the code below, the Client.KNE reference works, but ClientDiv does not, even though prior to that each, both are defined, populated variables…

By switching Client and ClientDiv to global variables, it works, but I feel like I should not have to create global variables here…

Doesn’t Work:

jQuery.each(Messages.Additions, function (clientIndex) {
    var Client = Messages.Additions[clientIndex];
    var ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex) {
            var KNE = Client.KNE[kneIndex];                       // Works
            var KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);  // DOES NOT WORK

Does Work:

jQuery.each(Messages.Additions, function (clientIndex) {
    Client = Messages.Additions[clientIndex];
    ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex) {
            KNE = Client.KNE[kneIndex];                       // Works
            KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);  // Works

Anyone know what I’m doing wrong in the first version? Or is this a bug? Why does the one variable work but the other doesn’t…

From here: Jquery $().each method obscures 'this' keyword it looks like I could pass the variables into the function call, but should I have to?

Tried the above link, and it is not working:

jQuery.each(Messages.Additions, function (clientIndex) {
    var Client = Messages.Additions[clientIndex];
    var ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex, Client, ClientDiv) {
            var KNE = Client.KNE[kneIndex];
            var KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);   //Does not work - ClientDiv undefined

Similar questions without satisfactory answer:
Scope of jQuery each() function?

SOLUTION

$.each(Messages.Additions, function () {
    var $Client = this;
    var $ClientDiv = $("#clientTitle_" + $Client.ClientID);

    if (!$ClientDiv.length) {
        $("#ClientTemplate").tmpl($Client).appendTo("#ClientContainer");
    } else {
        $.each($Client.KNE, function () {
            var $KNE = this;
            var $KNEDiv = $ClientDiv.find("#kneTitle_" + jq($KNE.KNE));    
            // SWITCHED TO $ PREFIX
  • 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-25T02:38:54+00:00Added an answer on May 25, 2026 at 2:38 am

    You can try this using this keyword which points to the current item in the loop. Instead of checking for if (ClientDiv == null) you should check for if (ClientDiv.length > 0) because jQuery returns am empty object if it do not finds the element so that check will fail.

    var additions;
    jQuery.each(Messages.Additions, function () {
        var $clientDiv = $("#clientTitle_" + this.ClientID);
    
        if ($clientDiv.length == 0) {
            $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
        } else {
            jQuery.each(Client.KNE, function () {
                $clientDiv.find("#kneTitle_" + this.KNE);
            });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, onreadystatechange call back function is not being called in asynchronous mode.
For some reason, Section 1 works but Section 2 does not. When run in
For some reason when I call FB.logout(); , the user does not actually get
For some reason, the following code will work: The following code does not work:
Below are some functions i have that use the official jQuery Template plugin and
Possible Duplicate: jquery ajax call taking too long or something for some reason this
I have a photo selection model, but for some reason whenever I call the
For some reason I never see this done. Is there a reason why not?
For some reason a call to header() causes an internal server error for me.
For some reason, I try to call AdjustWindowRect, or GetClientRect, and the parameters I

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.