Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few problems here.</p> <ol> <li>When you call jQuery with an array (like you're doing), jQuery expects it to be an array of DOM elements.</li> <li>If you want to use <code>$.each</code>, use the <a href="http://api.jquery.com/jquery.each/" rel="nofollow">static version</a> that iterates over generic objects or arrays.</li> </ol> <p>With both of those things said, there's <em>stil</em> an issue with using <code>$.each</code> with an array containing primitive values. The following code exhibits the same problem you were seeing:</p> <pre><code>$.each([".b"], function () { console.log($('.a').find(this).text()); // Expecting to print "Test" }); </code></pre> <p>Inside the callback function for <code>.each</code>, <code>this</code> is a <code>String</code> <em>object</em> and not a string <em>primitive</em>. To understand this, we need to look at what <code>.each</code> is doing under the hood:</p> <pre><code>for (; i &lt; length;) { if (callback.apply(object[i++], args) === false) { // &lt;---- break; } } </code></pre> <p>The important bit is the call to <code>apply</code>. According to the ECMAScript specification, when a primitive value is passed to <code>apply</code>, the value's <code>toObject</code> method is called:</p> <blockquote> <p>If thisArg is null or undefined, the called function is passed the global object as the this value. <strong>Otherwise, the called function is passed ToObject(thisArg)</strong> as the this value.</p> </blockquote> <p>This explains why your code was not working-- <code>.find</code> expects a string primitive and not a String object.</p> <p>This is why <a href="http://api.jquery.com/jquery.each/" rel="nofollow">the documentation for <code>$.each</code></a> actually mentions using <code>this</code> with primitive values:</p> <blockquote> <p>(The value can also be accessed through the this keyword, <strong>but Javascript will always wrap the this value as an Object even if it is a simple string or number value.</strong>).</p> </blockquote> <p>Therefore the way to fix the problem with your code is to leverage the <code>element</code> argument that's passed to the callback function:</p> <pre><code>$.each([".b"], function (_, item) { console.log($('.a').find(item).text()); // Expecting to print "Test" }); </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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