0

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

(function($){
    $.fn.plugin = function() {  
        return $(this).each(function(){
            var obj = $(this);
            obj.css('background', 'blue');
            alert(this);
        });
    };
})(jQuery);
2
  • For testing this, i included a bunch of li elements within a ul-list, selected them and checked the length of the matched elements altogether. The result was always euqal to 0. console.log($('li').length); Commented Aug 5, 2009 at 16:48
  • Wow thx for asking me how i call the plugin... The solution is quite simple, just as trivial as such a problem. I forgot to call to include the call into the following: $(document).ready(function() { $('li').plugin(); }); Commented Aug 5, 2009 at 16:54

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.