0

I want to run a an append(and more stuff later on) from within a plugin, but for some reason it doesn't do anything.

the code(basic):

(function($){   

    function _init(){

        $('body').append('<div class="msg"/>');

    }

    _init()             

})(jQuery);
4
  • Are you initializing this plugin correctly? Commented Mar 1, 2012 at 23:05
  • 2
    This is not really a plugin. It is the same as just defining and calling _init() immediately. Most likely document.body is not loaded yet at this point. It is not clear to me at which stage you want to execute which code. Commented Mar 1, 2012 at 23:05
  • This is a plugin but its not initialized at this stage, but i see that its because the DOM isn't ready.......stupid me :P Commented Mar 1, 2012 at 23:09
  • I mixed up a little. This isn't a jQuery plugin. My bad! Commented Mar 1, 2012 at 23:13

2 Answers 2

4

jQuery append can only work after dom ready. The code you have pasted is probably working before dom ready event. Please check below;

http://api.jquery.com/ready/

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

Comments

1

To put it in code, it should look like:

(function($){

    jQuery(document).ready(function(){

        $('body').append('<div class="msg"/>');

    });

})(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.