{"id":459,"date":"2022-08-30T15:01:42","date_gmt":"2022-08-30T15:01:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/array-of-selectors-for-loop-vs-each-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:42","modified_gmt":"2022-08-30T15:01:42","slug":"array-of-selectors-for-loop-vs-each-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/array-of-selectors-for-loop-vs-each-collection-of-common-programming-errors\/","title":{"rendered":"Array of selectors: for loop vs. $.each-Collection of common programming errors"},"content":{"rendered":"<p>Try<\/p>\n<pre><code>var arr = ['#home', '#news', '#tidy'];\n$(arr.join(',')).toggle();\n<\/code><\/pre>\n<p>$(arr.join(&#8216;,&#8217;)) =&gt; $(&#8216;#home, #news, #tidy&#8217;), which selects all 3 elements then toggle() operates on all the selected elements.<\/p>\n<p>If you do<\/p>\n<pre><code>$.each(arr, function(){\n    $(this).text(\"my id is \" + this + \".\");\n});\n<\/code><\/pre>\n<p>the <code>this<\/code> is actually a string object not a string primitive so what you get in return form <code>$(this)<\/code> is a jQuery object wrapping a string object not the element that the selector will match. Adding a string primitive <code>''<\/code> to a string object gives you a string primitive which is why it works that way.<br \/>\nIf you have to use <code>$.each<\/code> better to use the arguments passed to the callback function, the first argument is the index of the array and the second is the value at the index.<\/p>\n<pre><code>$.each(arr, function(index, value){\n    $(value).text(\"my id is \" + this + \".\");\n});\n<\/code><\/pre>\n<p>http:\/\/jsfiddle.net\/2TPLD\/<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:43:44. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Try var arr = [&#8216;#home&#8217;, &#8216;#news&#8217;, &#8216;#tidy&#8217;]; $(arr.join(&#8216;,&#8217;)).toggle(); $(arr.join(&#8216;,&#8217;)) =&gt; $(&#8216;#home, #news, #tidy&#8217;), which selects all 3 elements then toggle() operates on all the selected elements. If you do $.each(arr, function(){ $(this).text(&#8220;my id is &#8221; + this + &#8220;.&#8221;); }); the this is actually a string object not a string primitive so what you 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-459","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/459","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=459"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/459\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}