tags:

views:

35

answers:

2

I know this sounds pretty trivial, but the following function (return ($this).each()...`) seems to be everything but alive in my plugin, wich means that i even don't get an alert. Do you have any suggestions for a possible problem? Thx in advance.

(function($){
    $.fn.plugin = function() { 
     return $(this).each(function(){
      var obj = $(this);
      obj.css('background', 'blue');
      alert(this);
     });
    };
})(jQuery);
+1  A: 

How are you calling the plugin? Do you have matching elements? Check the .length on the selector to ensure you have > 0 matched elements.

redsquare
A: 

check this

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<script type="text/javascript">
    (function($){
     $.fn.plugin = function() {  
      return $(this).each(function(){
        var obj = $(this);
        obj.css('background', 'blue');
        $("#result").append('<span>in plugin : '+obj.html()+'</span><br />');
      });
     };
    })(jQuery);

    jQuery(function(){
     var test = $("div").plugin();

     $(test).each(function(){
      $("#result").append('<span>out plugin : '+$(this).html()+'</span><br />');
     });
    });

</script>
<head>
<body>
<div>a</div>
<div>b</div>
<div>d</div>
<div>v</div>
<span id="result"></span>
</body>
</html>
andres descalzo