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 8302305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:15:27+00:00 2026-06-08T17:15:27+00:00

Background I have a jQuery plugin I created, the plugin takes all table on

  • 0

Background

I have a jQuery plugin I created, the plugin takes all table on a page and loads the rows by making a remote request.

Code Example

This is a simple example that heights the structured used by the plugin. In the actual example the load function makes an AJAX request and processes the result adds rows to the table.

JavaScript

(function($) {

    $.table = function(el, options) {
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;

        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        base.load = function() {
            base.$el.find('tbody:last').append("<tr><td>...</td><td>...</td></tr>");
        };

        base.load();
    };

    $.fn.table = function(options) {
        options = options || {};
        return this.find(options.selector)
            .each(function(index) {
                new $.table(this, options);
        })
    };
})(jQuery);;

HTML

<table id="table1" data-role="table">
    <thead id="tour-table">
        <tr>
            <th data-id="id" width="20px">col1</th>
            <th data-id="given_name">col2</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

<table id="table2" data-role="table">
    <thead id="tour-table">
        <tr>
            <th data-id="id" width="20px">col1</th>
            <th data-id="given_name">col2</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

I initialise all the tables on a page like this.

$('body').table({selector:'[data-role="table"]'});

jsfiddle

You can see an example in action http://jsfiddle.net/RWy5r/1/

Question

Is it possible to expose the load function to the dom so that I can call it against the object something like this?

$("#table1").load();

or

$("#table1").table.load();

Using the simple example above the result would be that the table with ID "table1" would have the <td> appended and all other tables including the table with ID "table2" would remain unchanged.

  • 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-08T17:15:28+00:00Added an answer on June 8, 2026 at 5:15 pm

    This took me a little time to work out however the solution is very simple. All we need to do is add the object the plugin created to the dom so we can access it at a later time, jQuery already provides a way to do this via the .data() function.

    Solution

    (function($) {
    
        $.table = function(el, options) {
            var id = $(el).attr("id");
            $.data($(el).get(0), "table", this);//add the new object to the dom so we can access it later
            
            // To avoid scope issues, use 'base' instead of 'this'
            // to reference this class from internal events and functions.
            var base = this;
    
            // Access to jQuery and DOM versions of element
            base.$el = $(el);
    
            base.load = function() {
                
                base.$el.find('tbody:last').append("<tr><td>...</td><td>...</td></tr>");
            };
    
            base.load();
        };
    
        $.fn.table = function(options) {
            options = options || {};
            return this.find(options.selector).each(function(index) {
                    new $.table(this, options);
            })
        };
            
        //public method to provide access to the plugin object from the dom
        $.fn.table.load = function(selection) {
            $.data($(selection).get(0), "table").load();
        } 
            
        $('body').table({selector:'[data-role="table"]'});
            
        $('#btn1').click(function(){
            $.data($("#table1").get(0), "table").load();
        });
            
     
        $('#btn2').click(function(){
            $("#table2").table.load("#table2");
        });
    })(jQuery);​
    

    Take a look at a working example http://jsfiddle.net/leviputna/RWy5r/5/

    Explanation

    When the plugin is initialised I am storing the the object created against the dom object so I can access it later. $.data($(el).get(0), "table", this);

    Then I am exposing a public function that will find the object against a selection and call the load method.

    $.fn.table.load = function(selection) {
        $.data($(selection).get(0), "table").load();
    }
    

    Finally I added two on click functions to illustrate the process in action.

    $('#btn1').click(function(){
        $.data($("#table1").get(0), "table").load();
    });
    

    I hope this solution helps someone in the future.

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

Sidebar

Related Questions

I am using jQuery plugin supersized for full page background images (slideshow), I have
I'm using the jquery tabs plugin and want to have the background a light
I'm using jQuery validation plugin. I have two images: first background of label, when
I'm using the Tablesorter Jquery plugin to sort a table, but the images background
I have created a slider using jquery and the slides plugin . I wanted
I'm using Backstretch jQuery plugin to create full screen background images, but have a
Background I have been using the JQuery UI plugin for creating a dialog that
Background: I have a jQuery Datatable with 53x columns. The first column is the
I have this jQuery call for a dropdown. On click, the background image of
I have a problem with jQuery parallax effect for my body's background. Here is

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.