Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code is adding a "plugin" to jQuery that makes your function available on jQuery instances. The reason you're only seeing one ID in your function is that you're using <a href="http://api.jquery.com/attr/" rel="nofollow"><code>attr</code></a>, which only retrieves the <em>first</em> element's attribute. (Also, you <strong>really</strong> don't need <code>attr</code> to get an <code>id</code> value.)</p> <p>Your function should look something like this (<a href="http://jsbin.com/ujaxef" rel="nofollow">live copy</a>):</p> <pre><code>jQuery.fn.mytest = function () { var ids = jQuery.map(this, function(elm) { return elm.id; }); alert(ids.join(",")); }; </code></pre> <p>...to show the <code>id</code> values (if any) of each element in the currently-matched set.</p> <p>Or you could do it with a simple loop rather than with <code>jQuery.map</code> (<a href="http://jsbin.com/ujaxef/2" rel="nofollow">live copy</a>):</p> <pre><code>jQuery.fn.mytest = function () { var ids, index, elm; ids = []; for (index = 0; index &lt; this.length; ++index) { elm = this[index]; if (elm.id) { ids.push(elm.id); } } alert(ids.join(",")); }; </code></pre> <p>Also, note that within a jQuery plug-in function, <code>this</code> is the current jQuery instance, so you don't need (or want) to pass it through <code>$()</code> to create a jQuery wrapper around the element (you do that in event handlers, not plug-ins). So when I do <code>this.length</code> in the second example above, I'm using the <code>length</code> property of the jQuery instance that we're currently operating on. When I index into <code>this</code> using bracketed notation (<code>elm = this[index];</code>), I'm indexing into the jQuery instance's matched set of elements (like the <a href="http://api.jquery.com/get/" rel="nofollow"><code>get</code></a> method, but more directly).</p>
    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