bingjie2680
bingjie2680

Reputation: 7773

jQuery plugin does not run

I am just writing a simple plugin for my own use in a web page. I have html markup like:

<ul id="ulview">
    <li>...</li>
    <li>...</li>
    ...
</ul>

and jQuery plugin code:

(function($) {
    $.fn.ulView = function(){
       //console.log(this);
       this.children('li').each(function(){
           alert('test');
       });
    }
})(jQuery);

and I apply the plugin like this:

jQuery(document).ready(function($) {
    $('#ulview').ulView();
}

For some reason the alert('test') never runs. but I can see the jQuery object has been logged in the console. what do I miss here, thanks for any input.

Upvotes: 2

Views: 105

Answers (3)

dov.amir
dov.amir

Reputation: 11637

this works :

  $.fn.ulView = function(){
       //console.log(this);
       this.children('li').each(function(){
           alert('test');
       });
    }

$('#ulview').ulView();

(remove the external "function($) { ")

Upvotes: 2

frictionlesspulley
frictionlesspulley

Reputation: 12358

define the jquery plugin as

(function($) {
    $.fn.ulView = function(){
       //console.log(this);
       $(this).children('li').each(function(){
           alert('test');
       });
    }
})(jQuery);

Upvotes: 0

genesis
genesis

Reputation: 50966

(function($) {
    $.fn.ulView = function(){
       //console.log(this);
       this.children('li').each(function(){
           alert('test');
       });
    }
})(jQuery);

$(function(){

    $('#ulview').ulView();
});

http://sandbox.phpcode.eu/g/69be3.php

you forgot to execute your function (jQuery)

Read this

Upvotes: 3

Related Questions