Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The return of <code>this.each</code> is always <code>this</code> which is not modifiable and in your case is the list of selected elements that you call .Plugin() on. In your case, you do not want to simply return the list of elements that was selected...but an altered list. (You are 100% correct that your returns inside the .each loop aren't doing anything to affect the return of this.each itself.)</p> <p>If you define your variable outside the <code>this.each</code> loop to collect your new list of dom elements, <strong>add</strong> the correct dom elements inside the loop (see note 1), and then return the new list of elements outside the loop, you will see the results you want. However, there are several gotchas here.</p> <p>Note 1: The reason we use this.each for plugin processing is because the selectors can return multiple elements. You will need to collect a new list of elements since you are basically going to return a different set of elements than the selector selected whenever there is a non-p tag included. The case to consider is <code>$('div.myDiv, p.myP').Plugin()</code> which selects multiple elements with different tag types.</p> <p>One of the main gotchas is that we often set styles on the element we are selecting or even use the id to help identify the dom element that the plugin is attached to. By altering the dom element that is the root of the plugin there can be a lot of problems with location of css or ids that would need to be addressed or at least considered. </p> <p><em>(I am not suggesting that it cannot be done, especially if you are just converting something that you are already using versus building a plugin for general use by others--just that it brings up a lot of prickly issues.)</em></p> <pre><code>(function($) { var methods = { init: function(options) { var mes; this.each(function () { var me; if($(this).prop('tagName') != 'P') { $(this).append('&lt;p&gt;&lt;/p&gt;'); me = $(this).children('p'); } else me = $(p); } mes.push(me); return mes; }, ... other methods ... }; $.fn.Plugin = function (method) { if (methods[method]) { return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); } else if (typeof method === 'object' || !method) { // Run Init return methods.init.apply(this, arguments); } else { $.error('Method ' + method + ' does not exist on jQuery Plugin'); } }; } (jQuery)); </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