Rio Arfani
Rio Arfani

Reputation: 89

JQuery custom function with multiple parameter doesn't working

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

Upvotes: 1

Views: 40

Answers (1)

Praveen Gupta
Praveen Gupta

Reputation: 246

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);

Upvotes: 3

Related Questions