{"id":396,"date":"2022-08-30T15:00:39","date_gmt":"2022-08-30T15:00:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/retrieving-variables-from-a-jquery-each-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:00:39","modified_gmt":"2022-08-30T15:00:39","slug":"retrieving-variables-from-a-jquery-each-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/retrieving-variables-from-a-jquery-each-collection-of-common-programming-errors\/","title":{"rendered":"retrieving variables from a jquery each()-Collection of common programming errors"},"content":{"rendered":"<p>You could instead do <code>$('.box.highlight').attr('id');<\/code><\/p>\n<p>If you really want to access that variable OUTSIDE of the inner function used for <code>.each()<\/code> you need to define the variable outside of that function. Note that this won&#8217;t work quite the same way for asynchronous calls, but should with <code>.each()<\/code><\/p>\n<pre><code>var id;\n$('.box').each(function(){\n    if ($(this).hasClass('highlight')){\n        id = $(this).attr('id');\/\/get the id    \n        console.log('It has the class = ' + id);\/\/this shows correctly \n    }                       \n});\nconsole.log('id', id);\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 18:44:15. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>You could instead do $(&#8216;.box.highlight&#8217;).attr(&#8216;id&#8217;); If you really want to access that variable OUTSIDE of the inner function used for .each() you need to define the variable outside of that function. Note that this won&#8217;t work quite the same way for asynchronous calls, but should with .each() var id; $(&#8216;.box&#8217;).each(function(){ if ($(this).hasClass(&#8216;highlight&#8217;)){ id = $(this).attr(&#8216;id&#8217;);\/\/get [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-396","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}