Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery .each() Scope Problem (bug?)
    primarykey
    data
    text
    <p>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...</p> <p>By switching Client and ClientDiv to global variables, it works, but I feel like I should not have to create global variables here...</p> <p>Doesn't Work:</p> <pre><code>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 </code></pre> <p>Does Work:</p> <pre><code>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 </code></pre> <p>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...</p> <p>From here: <a href="https://stackoverflow.com/questions/980821/jquery-each-method-obscures-this-keyword">Jquery $().each method obscures &#39;this&#39; keyword</a> it looks like I could pass the variables into the function call, but should I have to?</p> <p>Tried the above link, and it is not working:</p> <pre><code>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 </code></pre> <p>Similar questions without satisfactory answer: <a href="https://stackoverflow.com/questions/1397062/scope-of-jquery-each-function">Scope of jQuery each() function?</a></p> <p><strong>SOLUTION</strong></p> <pre><code>$.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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload