Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8865925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:38:58+00:00 2026-06-14T16:38:58+00:00

I am trying to create a Jquery plugin that maintains chainability and has public

  • 0

I am trying to create a Jquery plugin that maintains chainability and has public methods as specified in Jquery Plugins/Authoring . The complexity is that it is trying to maintain certain vars that I want the public methods to use.

This is my jsfiddle : http://jsfiddle.net/badmash69/9cqcj/2/

javascript code :

(function($){

  var methods = {
    init : function( options ) {
      this.options = options;
    }
  , add_that: function (elem) {

      $(this).append(elem);
      return (this);
    }
  , show_parent: function(){
      // this is a simple test to see if the plugin vars are accessible
      alert("parent id=" + $(this).parentId)
    }              
  , add_this: function (elem) {
      return methods.add_that.apply(this,elem);
    }
  };

  $.fn.test = function (method) { 
        var args = method;
        var argss = Array.prototype.slice.call(args, 1);      


      return this.each(function(){

          var $this = $(this);
          if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' + method + ' does not exist on jQuery.test' );
    }          


          var element = $(this);
          var parentId= element.parent().attr("id")

      });       


  };

})(jQuery);

$('#test').test('add_this',$('<div>Hello World d</div>'));

$('#test').test('show_parent');
​

Html Code

<div id="holder">
<div id="test"></div>
</div>  

I cant figure out what I am doping wrong here .
How can I make it work ? I would deeply appreciate any help .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:38:59+00:00Added an answer on June 14, 2026 at 4:38 pm

    Take a look at this demo: http://jsfiddle.net/9cqcj/11/

    As they suggest, to keep data you should better use .data:

          return this.each(function(){          
              var $this = $(this);
              $this.data("parentId",$this.parent().attr("id"));
    ....
    

    (assuming that you need parentId of each element in set)

    Also, you have a problem with calling your methods:

     return this.each(function(){
    
              var $this = $(this);
              if ( methods[method] ) {
          return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    

    Last line, arguments – arguments of function passed to .each is used. In order to get original arguments save them into variable before calling a method:

    $.fn.test = function (method) { 
            var args = arguments;      
          return this.each(function(){          
              var $this = $(this);
              $this.data("parentId",$this.parent().attr("id"));
              if ( methods[method] ) {
          return methods[ method ].apply( this, Array.prototype.slice.call( args , 1 ));
    

    See arguments replaced with args in last line.

    Also, when you are using .apply, second parameter should be an array:

    return methods.add_that.apply(this, [elem]);
    

    In case like this:

    return methods.add_that.apply(this, elem);
    

    You can get unexpected problems. For instance, try to replace elem with simple string "test" and see what you will get in console. Or if you will pass jQuery object, you will get DOM object in called method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm am trying to create a jQuery plugin that will add new namespace functions
I am trying to create a plugin very that has similar usage to the
I'm trying to dynamically create alert messages using the jQuery plugin for Bootstrap CSS.
I am trying to create a jQuery Gallery that lets the user flip (with
I'm trying to create a jQuery selector that will do something like this: (.a
I'm trying to create a JQuery function that will test a string for the
I am trying to create a jquery plugin to do ajax table pagination (i.e
I trying to create an animation with JQuery that scrolls 1 word in a
I'm using the JQuery Draggable plugin . I'm trying to create an Image Editing
i'm trying to create my first jQuery plugin. i got a prototype function called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.