1

I tried to create JQUERY to append div to existing div

(function($){
  $.fn.alertme=function(){
    var opts = $.extend( {}, $.fn.alertme.defaults, options );
    $divContent =$('<div></div>').appendTo(opts.container);
    $divContent.prop('class','divclass');
  };

  $.fn.alertme.defaults = {
    container: "body",
    background: "yellow"
  };
})(jQuery);

this is how i called it:

$(document).ready(function(){
  $('#testbutton').on('click',function(){
    $.alertme(
      {container : '#target'}
    );
  });
});

its return error options in not define

please help me guys what i do wrong here

6
  • You can use $("#target").alertme() to calling function and use this instead of opts.container in function defintion Commented Dec 18, 2018 at 8:17
  • yes, but i need to fire the plugin from dynamic content Commented Dec 18, 2018 at 8:21
  • $("#target"). is selector and you can select any element you want Commented Dec 18, 2018 at 8:22
  • i need to call it with button click, Commented Dec 18, 2018 at 8:24
  • Check jsfiddle.net/ytfzmup1 Commented Dec 18, 2018 at 8:26

1 Answer 1

3

Pass options as argument.

(function($){
  $.extend({
    alertme: function(options){
      var defaults = {
        container: "body",
        background: "yellow"
      };
      var opts = $.extend( {}, defaults, options );
      $divContent =$(opts.container).append('<div>abc</div>');
      $divContent.prop('class','divclass');
    }
  });
})(jQuery);
Sign up to request clarification or add additional context in comments.

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.