0

my plugin

$.fn.myplugin=function(){

var element=$('<div/>').addClass('select').appendTo(this);

return this;
}

and script

$('<div/>').myplugin().appendTo('body');

problem is element not appending.

3 Answers 3

1

It works as is: http://jsfiddle.net/7n2Bd/

But you will have a problem if you are passing a collection of elements. Try this:

$.fn.myplugin=function() {
    return this.each(function() {
        $('<div>').addClass('select').appendTo(this);
    });
};
Sign up to request clarification or add additional context in comments.

Comments

1

add a document ready could help here too

    $(document).ready(function() {
            $('<div/>').myplugin().appendTo('body');
    });

Comments

1

It seems you code is OK.

$.fn.myplugin = function() {
    // hello is for just view purpose
    $('<div>hello</div>').addClass('select').appendTo(this);
    return this;
}

$('<div/>').myplugin().appendTo('#target')​; // here instead of '#target' use 'body'

DEMO

For collection

$.fn.myplugin = function() {
    return $.each(this, function() {
       $('<div>hello</div>').addClass('select').appendTo(this);
    });        
}

Note

Put all your code within $(document).ready({ .. }).

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.