Jquery problem: each only uses last object
<p>I have to following code in HTML:</p>

<pre><code>$(document).ready(function(){
 jQuery(".test").each(function(index) {
  jQuery(this).someObject({attr : index});
 });
});
</code></pre>

<p>someObject looks like this <strong><em>(see Edit2.5)</em></strong></p>

<p>I have an onclick event on that object. If it is clicked it executes the function "someFunc" (which it does).</p>

<p>The Problem is, it alerts only the last index. Let's say I have 5 objects with the class ".test". If I click any of the someObject it created, it will always alert index of the last object.</p>

<p><strong>Edit:</strong>
Ok I forgot an important detail. In the 'someObject' I create a div which contains a table and each td has an onclick function which would execute the function 'someFunc'.</p>

<p>If I make 'someFunc' privat with this:</p>

<pre><code>var someFunc = function(){
  alert(settings.attr);
}
</code></pre>

<p>The browser creates an error "Object expected".</p>

<p><strong>Edit2.5</strong>: The entire 'someObject'</p>

<pre><code>(function($){  
    $.fn.someObject = function(options){

        // Set default Settings
        var settings = {
            dpButtonImage : "images/icon.png",
            dpButtonClass : "Button",
            openOnFocus : false
        };

        $.extend(settings, options);

        var someFunc = function(){
            alert(settings.arr);
        }

        var displayPicker = function(target, counter){
            var offset = jQuery(target).offset();
            var height = jQuery(target).outerHeight();

            drawPicker(target, offset, height);
        }

        var drawPicker = function(target, offset, height){
            if (!document.getElementById("Wrapper")){
                $("body").append(function(){
                    return $("&lt;div&gt;&lt;/div&gt;")
                        .addClass("Wrapper")
                        .attr({
                            id : "Wrapper"
                        })
                        .css({
                            display : "none"
                        });
                });
            }

            // Create container element.
            var container = $("#Wrapper");

            // Move the wrapper to the proper coordinate.
            container.css({
                top: offset.top + height + 1,
                left: offset.left
            });

            // Toggle the visibility.
            if (container.is(":visible")){
                container.hide();
            } else {
                container.show();
            }

            refreshPicker2();
        }

        refreshPicker2 = function(){
            var html = $("&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Test&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;");
            html.find("td").click(someFunc);
            $("#Wrapper").html(html);
        }

        return this.each(function(){
            var target = this;
            var $target = $(this);
            var counter = $.event.guid++;
            var readonly = $(target).attr("readonly");

            target.settings = settings;

            if (target.settings.openOnFocus){
                $target.bind("focus", function(){
                    if (!readonly){ displayPicker(target, counter); }
                });
            }

            // Append dpButton if it doesn't already exist.
            if (!document.getElementById('dpButton_'+counter)){
                $target.after(function(){
                    return $("&lt;img /&gt;")
                        .addClass(target.settings.dpButtonClass)
                        .attr({
                            src : target.settings.dpButtonImage,
                            alt : "testicon",
                            id  : "dpButton_"+counter
                        })
                        .bind("click", function(){
                            if (!readonly){ displayPicker(target, counter);}
                            return false;
                        });
                });
            }
        });
    };  
})(jQuery);
</code></pre>
