2

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.

3 Answers 3

3
(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

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

2 Comments

I said: 'but I can see the jqury object has been logged in the console.' ...that means everything runs..except the one...
sorry I have the (jquery) in place. I forgot to place it here
2

this works :

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

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

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

Comments

0

define the jquery plugin as

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

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.