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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:42:56+00:00 2026-05-30T02:42:56+00:00

I am learning JQuery. I have a need to create a custom control. This

  • 0

I am learning JQuery. I have a need to create a custom control. This control is going to basically render some HTML. Sometimes, I want to just get the HTML. My hope is to use the following syntax:

// Put generated html inside of "myElement". "myElement" is a div element.
$("#myElement").myPlugin({ value: 10 });

// Retrieve the html that myPlugin would place of a div element.
// Basically, I want the javascript equivalent of a C# static function here.
// But I think the following approach is wrong:
var html = myPlugin().getHtml(10); 
alert(html); 

In an attempt to accomplish this, I’m using the following:

(function ($) {
  $.fn.myPlugin = function (element, options) {
    var defaults = { theValue: 0 }

    var plugin = this;
    plugin.settings = {};

    var $element = $(element),
    element = element;

    plugin.init = function () {
      plugin.settings = $.extend({}, defaults, options);
      var html = createHtml(defaults.theValue);
      $(element).html(html);      
    }

    plugin.getHtml = function (v) {
      return createHtml(v);
    }

    var createHtml(v) {
      return "<span>" + v + "</span>";
    }
})(jQuery);

I have two problems: 1) I can see that the HTML is being generated, but it does not appear to be added to the DOM. 2) I can’t statically call the function. How can I make my function statically visible?

Thank you!

  • 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-05-30T02:42:58+00:00Added an answer on May 30, 2026 at 2:42 am

    There are a few tweaks you need to make:

    (function($) {
        $.fn.myPlugin = function(options) {
            var defaults = {
                theValue: 0
            },
                settings = $.extend({}, defaults, options);
    
            this.html($.myPlugin.getHtml(settings.theValue));
        };
    
        $.myPlugin = {
            getHtml: function(value) {
                return "<span>" + value + "</span>";
            }
        };
    
    })(jQuery);
    

    Usage:

    $("#foo").myPlugin({ theValue: 10 });
    var html = $.myPlugin.getHtml(10);
    
    • First off, you were never calling the init method, so nothing was going to work. It isn’t going to be called automatically. Perhaps you were thinking of the jQueryUI widget factory?
    • Additionally, element doesn’t get passed to the plugin, this refers the the element the plugin was called on. You only get passed options.
    • Finally, to create a static jQuery method, just attach it right to $.

    Example: http://jsfiddle.net/xF2S6/

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

Sidebar

Related Questions

I'm trying to create a custom event with jquery 1.7, but I'm just learning
In my learning I have always learnt and understood callbacks in jQuery like this:
For learning jQuery UI dialog, I have the code defined below. I need to
I'm still learning jQuery and would appreciate some pointers with this, as I think
I'm learning JQuery and have found the factory function $() with its selectors quite
I have just started learning Jquery and am new to writing javascript (I am
I am learning JQuery, and I have ran into an odd issue. I made
I'm learning jQuery and am about to write some pages using intensively that library.
Been trying this for quite a while now and I need help. Basically I
I have a jquery function(This is not written by me anyway still I am

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.